ruby-on-rails - rspec 升级到 3.1.0 后,与 allowed_any_instance_of 一起使用的 and_call_original 不起作用

标签 ruby-on-rails rspec

我在 rspec 2.14.1 中有一个代码,例如

allow_any_instance_of(AnyClass).to receive(:some_method).and_call_original

具有相应的消息期望

expect_any_instance_of(AnyClass).to receive(:some_method)

以上在 rspec 2.14.1 中运行良好。升级到rspec 3.1.0后,上述代码不再起作用。它未能满足消息期望,some_method 甚至没有被调用一次

但是,如果我像这样更改 stub

allow_any_instance_of(AnyClass).to receive(:some_method).and_return(value)

它在 rspec 3.1.0 中工作正常。

我只是想了解为什么在 rspec 升级后将 and_call_original 与allow_any_instance_of 一起使用会失败。

我可以从this link看到and_call_original 仅支持部分 double 。

这是否意味着使用 allow_any_instance_of 不是部分 double ?

最佳答案

and_call_original 在与 allow_any_instance_of 一起使用时确实有效。

请参阅 any_instance 的规范,它让我们了解不同的模拟方法或使用 and_call_original 取消 stub here .

为了回答我上面的问题,我使用消息期望的方式是错误的。应该是:

allow_any_instance_of(AnyClass).to(
  receive(:any_method).and_call_original
)
expect(AnyClass.new.any_method).to eq(:any_method_value)

我试图使用expect_any_instance_of而不是expect导致了这个问题。

关于ruby-on-rails - rspec 升级到 3.1.0 后,与 allowed_any_instance_of 一起使用的 and_call_original 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52838223/

相关文章:

ruby-on-rails - rails 生成器的前缀

css - 为什么这个 <div> 比它的内容宽?

python - @Rails 用户 : have you tried web2py? 优点?缺点?

ruby-on-rails - 何时以及何时不 stub /模拟测试

ruby-on-rails - rails : how to display image from upload

mysql - 使用现有数据库 MySQL 的 Rails 应用

ruby-on-rails - Rspec 测试由于一个奇怪的原因而失败

ruby-on-rails - rspec 和 recaptcha

ruby-on-rails - 有测试血统的工厂

ruby-on-rails - Rspec 测试以 'rspec' 失败,但以 'rspec path_of_the_file' 失败