should_receive 和 should_not_receive 的 RSpec 都通过异常 [英] RSpec for should_receive and should_not_receive both passed for Exception
问题描述
我有一个非常奇怪的 rspec 案例场景.我试图测试我的函数是否正确处理异常.以下是我的代码:
I had a really weird rspec case scenario. I tried to test if my function handles exception correctly. And the following is my code:
在 User.rb 中:
in User.rb:
def welcome_user
begin
send_welcome_mail(self)
rescue Exception => exception
ErrorMessage.add(exception, user_id: self.id)
end
end
end
在 user_spec.rb 中
in user_spec.rb
it "adds to error message if an exception is thrown" do
mock_user = User.new
mock_user.stub(:send_welcome_mail).and_raise(Exception)
ErrorMessage.should_receive(:add)
mock_user.welcome_user
end
测试通过了,但是当我把ErrorMessage.should_receive(:add)
改成ErrorMessage.should_not_receive(:add)
时,它也通过了,有什么见解吗?>
The test passed, but when I change ErrorMessage.should_receive(:add)
to ErrorMessage.should_not_receive(:add)
, It also passed, any insights?
推荐答案
自从 rspec 2.11 暴露了我的一个测试表现出这种异常",我决定在 github 上提出这个问题.您可以关注 https://github.com/rspec/rspec-mocks/issues/164
Since rspec 2.11 exposed one of my tests to exhibit such "abnormality", I decided to raise the issue on github. You may following the discussion at https://github.com/rspec/rspec-mocks/issues/164
总结:any_instance.should_not_receive未定义,避免
Summary: any_instance.should_not_receive is undefined, avoid
这篇关于should_receive 和 should_not_receive 的 RSpec 都通过异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!