存根中any_number_of_times方法的替代

时间:2013-10-07 14:13:07

标签: mocking ruby-on-rails-4 rspec2 rspec-rails stubs

我已将项目升级到rails 4但现在我收到一些弃用警告,其中一个是 DEPRECATION:any_number_of_times已弃用。。我收到此警告的代码是

sponsorship = RSpec::Mocks::Mock.new(:sponsorship)

SPONSORSHIP.should_receive(:[]).with('sponsorship').any_number_of_times.and_return(sponsorship)

另一种情况是

sponsorship.should_receive(:[]).with(key).any_number_of_times.and_return(value)

我在上面的代码中使用了stub,但它没有正确存根。你能找到我做错的地方吗?对于存根,我使用了

SPONSORSHIP.stub(:[]).with('sponsorship').and_return(sponsorship)

2 个答案:

答案 0 :(得分:14)

方法any_number_of_times已被弃用(并且将在RSpec 3中消失),因为它并没有真正测试任何东西。它永远不会失败,因为它也可以被调用0次。请参阅https://trello.com/c/p2OsobvA/78-update-website-menu-architecture-to-accommodate-pledging-as-well-as-weddings-memorials-etc中的扩展参数。

如果您希望至少调用一次,则可以使用at_least(1).times

答案 1 :(得分:2)

由于 any_number_of_times 对其他替代方法没有任何帮助,例如 at_least(n) at_most(n) 帮助删除了这些弃用警告。

相关问题