RSpec 3.7.0中not_to receive与receive.never是否完全等价?
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.methodis 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

