Rspec let 助手中的 yield
Rspec yield in let helper
如题。是否有可能向 'let' 产生一些东西?喜欢这里:
let(:call_request) { post :create, article: FactoryGirl.attributes_for(yield) }
当我尝试使用它时:
it 'creates a new article' do
expect { call_request { :article } }.to change(Article, :count).by(1)
end
它说:
ArticlesController POST create when admin signed in with valid attributes creates a new article
Failure/Error: let(:call_request) { post :create, article: FactoryGirl.attributes_for(yield) }
LocalJumpError:
no block given (yield)
# ./spec/controllers/articles_controller_spec.rb:61:in `block (3 levels) in <top (required)>'
我能以某种方式做到吗?也许语法错误或者有更聪明的方法来做到这一点?
只需定义一个方法:
def call_request
post :create, article: FactoryGirl.attributes_for(yield)
end
let
和普通方法之间的唯一区别是 let
声明是每个示例记忆的。由于您打算将一个块传递给它,因此记忆化是不合适的,无论如何,方法 def 会更好。
如题。是否有可能向 'let' 产生一些东西?喜欢这里:
let(:call_request) { post :create, article: FactoryGirl.attributes_for(yield) }
当我尝试使用它时:
it 'creates a new article' do
expect { call_request { :article } }.to change(Article, :count).by(1)
end
它说:
ArticlesController POST create when admin signed in with valid attributes creates a new article
Failure/Error: let(:call_request) { post :create, article: FactoryGirl.attributes_for(yield) }
LocalJumpError:
no block given (yield)
# ./spec/controllers/articles_controller_spec.rb:61:in `block (3 levels) in <top (required)>'
我能以某种方式做到吗?也许语法错误或者有更聪明的方法来做到这一点?
只需定义一个方法:
def call_request
post :create, article: FactoryGirl.attributes_for(yield)
end
let
和普通方法之间的唯一区别是 let
声明是每个示例记忆的。由于您打算将一个块传递给它,因此记忆化是不合适的,无论如何,方法 def 会更好。