有没有更好的方法来检查RSpec中是否存在记录?
Foo.where(bar: 1, baz:2).count.should == 1
Run Code Online (Sandbox Code Playgroud)
chr*_*035 39
使用Rspec 2.13.0,我能够做到
Foo.where(bar: 1, baz: 2).should exist
Run Code Online (Sandbox Code Playgroud)
编辑:
Rspec现在有一个期望的语法:
expect(Foo.where(bar: 1, bax: 2)).to exist
Run Code Online (Sandbox Code Playgroud)
Mug*_*ica 12
对于rspec-rails> 3.0
有博客模型,
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
Run Code Online (Sandbox Code Playgroud)
使用expect语法:
expect(Foo.where(bar: 1, baz: 2)).not_to be_empty
expect(Foo.where(bar: 1, baz: 2)).to exist
Run Code Online (Sandbox Code Playgroud)