Ruby Rspec should_not_receive不起作用



对于下面的代码片段:

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

任何有关这方面的信息将不胜感激。

这样写:

expect(@by_hidden).not_to receive(:by_limit).with(100)

或者(取决于您的用例(甚至只是:

expect(@by_hidden).not_to receive(:by_limit)
如果您
  1. 不希望调用该方法,则存根返回值是没有意义的。
  2. expect(...).to语法多年来一直优于should/should_not。后一种方法对于某些类型的对象可能会有问题,因此最好避免使用。

最新更新