Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 316
rafamvc Avatar asked Feb 03 '11 01:02

rafamvc


4 Answers

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

like image 61
Jonah Avatar answered Nov 05 '22 10:11

Jonah


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.

like image 33
Don Roby Avatar answered Nov 05 '22 10:11

Don Roby


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
like image 2
Shawn I Avatar answered Nov 05 '22 10:11

Shawn I


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
like image 2
RamRovi Avatar answered Nov 05 '22 10:11

RamRovi