Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec: Expecting a message multiple times but with differing parameters

I currently have some expectations set up on a mock with consecutive calls:

The spec:

@my_mock = mock("a_mock")
@options1 = {:some => "option"}
@options2 = {:some_other => "option"}
@first_param = mock("first_param")

@my_mock.should_receive(:a_message).with(@first_param, @options1)
@my_mock.should_receive(:a_message).with(@first_param, @options2)

However, i get the following:

Mock "a_mock" received :a_message with unexpected arguments
  expected: (#<Spec::Mocks::Mock:0x81b8ca3c @name="first_param"{:some => "option"})
   got: (#<Spec::Mocks::Mock:0x81b8ca3c @name="first_param">, {:some_other => "option"})

When I debug this, the first expectation IS getting called. Do I have to specify anything else before I can expect consecutive calls with the same message but differing parameters?

like image 684
manlycode Avatar asked Nov 23 '09 19:11

manlycode


1 Answers

Try creating your mock as a null object to ignore extra method calls. Each of your expectations will still have to be met, but they won't step on each other.

@my_mock = mock("a_mock").as_null_object

This follows the Null Object pattern, in which any extraneous messages are just ignored. It is useful with mocks when you want to make sure a method gets called with certain parameters, but you don't care if it is called with other parameters or if any other methods get called.

like image 55
Baldu Avatar answered Nov 05 '22 13:11

Baldu