Is there a "not_expects" for mocha/rspec?
Asked Answered
A

4

17

I need to make sure a method is not called giving a specific set of conditions, and I'm looking for the opposite of the mocha expects.

Aluminize answered 3/2, 2011 at 1:5 Comment(0)
H
29

Look at mocha's never or rspec's should_not_receive and should_receive(:selector).exactly(n).times

Harrar answered 3/2, 2011 at 1:23 Comment(1)
Mocha link is outdated. New documentation on neverBehalf
D
3

I'm not a mocha expert by any means, but I suspect what you need may be supplied by a never modifier for an expectation.

Dorton answered 3/2, 2011 at 1:21 Comment(0)
S
3

Mocha example from the documentation

object = mock()
object.expects(:expected_method).never
object.expected_method # => unexpected invocation

object = mock()
object.expects(:expected_method).never
# => verify succeeds
Soulsearching answered 9/5, 2019 at 7:13 Comment(0)
H
2

RSpec 3.6 now handles this with expect(...).not_to receive(...).

From the link:

RSpec.describe "A negative message expectation" do
  it "passes if the message is never received" do
    dbl = double("Some Collaborator").as_null_object
    expect(dbl).not_to receive(:foo)
  end
end
Hagood answered 8/5, 2017 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.