You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RSpec 3.7.0中not_to receive与receive.never是否完全等价?

Are expect(instance).not_to receive(:method) and expect(instance).to receive(:method).never identical in RSpec 3.7.0?

Great question! Let’s break this down specifically for RSpec 3.7.0:

Short Answer

These two syntaxes are functionally identical—there are no meaningful differences in behavior, side effects, or how RSpec enforces the expectation.

Longer Explanation

Under the hood, RSpec’s never matcher is just an alias that maps directly to the not_to receive(...) logic. When you write:

expect(instance).to receive(:method).never

RSpec internally translates this to the exact same check as:

expect(instance).not_to receive(:method)

Both expectations will:

  • Fail immediately if instance.method is called at any point during the test example
  • Pass only if the method is never invoked
  • Handle argument matchers (like with(args)) identically. For example:
    # These two are equivalent too
    expect(instance).not_to receive(:method).with(123)
    expect(instance).to receive(:method).with(123).never
    

Stylistic Note Only

The only difference is purely stylistic: never is a readability tweak for developers who prefer framing the expectation in positive terms ("expect to receive... never") rather than negative ones ("expect not to receive..."). Pick whichever makes your test code clearer to you and your team.


内容的提问来源于stack exchange,提问作者Alexander Presber

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:14:04