rafamvc
rafamvc

Reputation: 8357

Is there a "not_expects" for mocha/rspec?

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.

Upvotes: 17

Views: 11154

Answers (4)

RamRovi
RamRovi

Reputation: 1016

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

Upvotes: 3

Shawn I
Shawn I

Reputation: 53

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

Upvotes: 2

Jonah
Jonah

Reputation: 17958

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

Upvotes: 29

Don Roby
Don Roby

Reputation: 41165

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.

Upvotes: 3

Related Questions