【问题标题】:Ruby Rspec should_not_receive not workingRuby Rspec should_not_receive 不工作
【发布时间】: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


【解决方案1】:

这样写:

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。对于某些类型的对象,后一种方法可能会出现问题,因此最好避免使用。

【讨论】:

  • 感谢您的建议
猜你喜欢
  • 1970-01-01
  • 2014-12-16
  • 2015-06-01
  • 1970-01-01
  • 2011-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多