Có cách nào tốt hơn để kiểm tra sự tồn tại của một bản ghi trong RSpec không?Cách kiểm tra sự tồn tại của một bản ghi trong RSpec
Foo.where(bar: 1, baz:2).count.should == 1
Có cách nào tốt hơn để kiểm tra sự tồn tại của một bản ghi trong RSpec không?Cách kiểm tra sự tồn tại của một bản ghi trong RSpec
Foo.where(bar: 1, baz:2).count.should == 1
Foo.where(bar: 1, baz: 2).exists?.should be_true
sử dụng Foo.exists?(bar: 1, baz: 2).should be_true
Với Rspec 2.13.0, tôi đã có thể làm
Foo.where(bar: 1, baz: 2).should exist
Edit:
RSpec hiện có an expect syntax:
expect(Foo.where(bar: 1, bax: 2)).to exist
'mong đợi (Foo.find_by_bar (1)). Để be_present' khi bạn đang sử dụng công cụ tìm – Andrei
Sử dụng cú pháp mong đợi:
expect(Foo.where(bar: 1, baz: 2)).not_to be_empty
expect(Foo.where(bar: 1, baz: 2)).to exist
Đối rspec-ray> 3,0
Có một mô hình Blog,
describe 'POST #create' do
it "creates a post" do
post :create, :blog => { title: "blog title"}
#returns true if the post was successfully added
expect(Blog.where(title: "blog title")).to be_present
end
end
Bạn có thể thử ' Foo.where (thanh: 1, baz: 2) .should_not be_empty'? – Dogbert