16

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.

Vega
  • 27,856
  • 27
  • 95
  • 103
rafamvc
  • 8,227
  • 6
  • 31
  • 34

4 Answers4

28

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

p.matsinopoulos
  • 7,655
  • 6
  • 44
  • 92
Jonah
  • 17,918
  • 1
  • 43
  • 70
  • 2
    Mocha link is outdated. [New documentation on never](http://gofreerange.com/mocha/docs/Mocha/Expectation.html#never-instance_method) – Seth Bro Oct 01 '14 at 03:19
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.

Don Roby
  • 40,677
  • 6
  • 91
  • 113
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
RamRovi
  • 986
  • 9
  • 13
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
Shawn I
  • 53
  • 7