【发布时间】:2020-02-13 22:19:17
【问题描述】:
对于下面这段代码sn-p:
@by_hidden.should_not_receive(:by_limit).with(100).and_return(@by_limit)
我正面临错误
@by_hidden.should_not_receive(:by_limit).with(100).and_return(@by_limit)
(Double Object).by_limit(100)
expected: 1 time with arguments: (100)
received: 0 times with arguments: (100)
对此的任何信息将不胜感激。
【问题讨论】:
-
should_not_receive已弃用,请使用expect(...).not_to receive(...)。另外,如果您不希望调用该方法,为什么要设置返回值?and_return毫无意义。 -
另外 -- 如果可能,请尝试在所有问题中提供minimal reproducible example。在这种情况下,minimal reproducible example 将是:一个完整的规范和一个基本的方法实现,其他任何人都可以运行它来获得与您展示给我们相同的错误。这只需要大约 5-10 行代码。
-
1.您应该使用@arieljuod 提到的语法。 2.你打电话了吗?如果您使用的是
expect(..).not_to receive(...),则需要在之后进行调用,从而在调用之前写入期望:expect(..).not_to receive(...); do_the_call;如果您想采用“给定,何时,然后”的方法,则需要按原样使用expect(...).not_to have_received(...)显示here -
感谢您的建议
标签: ruby-on-rails ruby ruby-on-rails-3 rspec rspec-rails