在rspec测试中找不到stubbed方法(Stubbed method can't be found in rspec test)

编程入门 行业动态 更新时间:2024-10-25 23:25:41
在rspec测试中找不到stubbed方法(Stubbed method can't be found in rspec test)

在我的控制器中,我有像......的代码

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) end

In 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 end

When 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_job

Any 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: :json

expect_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

更多推荐

本文发布于:2023-07-30 07:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336737.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   方法   测试中   rspec   stubbed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!