在我的控制器中,我有像......的代码
def apply ...do some validation stuff jobapp = JobApplication.new jobapp.apply_for_job(params, job) end在我的测试中,我想确保在所有验证通过之后,调用apply_for_job方法,因此我进行了以下测试。
describe 'apply' do before(:each) do @file = Rack::Test::UploadedFile.new(Rails.root.join('spec/fixtures/files/test-resume.txt'), 'plain/text') allow_any_instance_of(JobApplication).to receive(:apply_for_job).and_return(true) end it 'assuming all validation passes, it calls the jobapplication apply_for_job method' do post :apply, file: @file, job_id: 1, format: :json expect_any_instance_of(JobApplication).to receive(:apply_for_job) end end当我运行我的测试时,我得到了这个错误。
Failure/Error: Unable to find matching line from backtrace Exactly one instance should have received the following message(s) but didn't: apply_for_job有什么想法吗? 谢谢。
In my controller I have code like...
def apply ...do some validation stuff jobapp = JobApplication.new jobapp.apply_for_job(params, job) endIn my test I want to make sure that after all the validation passes, that the apply_for_job method was called so I have the following test.
describe 'apply' do before(:each) do @file = Rack::Test::UploadedFile.new(Rails.root.join('spec/fixtures/files/test-resume.txt'), 'plain/text') allow_any_instance_of(JobApplication).to receive(:apply_for_job).and_return(true) end it 'assuming all validation passes, it calls the jobapplication apply_for_job method' do post :apply, file: @file, job_id: 1, format: :json expect_any_instance_of(JobApplication).to receive(:apply_for_job) end endWhen I run my test I get this error.
Failure/Error: Unable to find matching line from backtrace Exactly one instance should have received the following message(s) but didn't: apply_for_jobAny ideas why? Thanks.
最满意答案
expect_any_instance 设置执行代码expect_any_instance 的期望 。 它没有验证对间谍的期望。
您正在设置测试代码运行后的期望值。 将期望定义向上移动一行:
expect_any_instance_of(JobApplication).to receive(:apply_for_job) post :apply, file: @file, job_id: 1, format: :jsonexpect_any_instance expect-any-instance-of-to-set-a-message-expectation-on-any-instance" rel="nofollow">sets the expectation at the time that line of code is executed. It does not verify an expectation on a spy.
You are setting the expectation after your code under test has run. Move the expectation definition up one line:
expect_any_instance_of(JobApplication).to receive(:apply_for_job) post :apply, file: @file, job_id: 1, format: :json更多推荐
发布评论