Stub (...) received unexpected message (...) with (no args)

ciembor picture ciembor · Apr 16, 2013 · Viewed 8.5k times · Source

I try to write a test using RR. What I need is a stub of a model object.

describe ApplicationController do

  subject(:application_controller)     { ApplicationController.new }
  let(:messages)                       { ['a1', 'a2', 'a3' ] }
  let(:model)                          { Object.new }

  it 'should copy errors to flash' do
    stub(model).error_messages { messages }
    flash[:error] == nil
    subject.copy_errors_to_flash(model)
    flash[:error].should == messages
  end

end

What I get is

ApplicationController should copy errors to flash
     Failure/Error: stub(model).error_messages { messages }
       Stub #<Object:0x007ffaa803f930> received unexpected message :error_messages with (no args)
     # ./spec/controllers/application_controller_spec.rb:10:in `block (2 levels) in <top (required)>'

I have no idea what am I doing wrong. I think I follow the docs...

Answer

mcfinnigan picture mcfinnigan · Apr 16, 2013

You're calling the method 'error_messages on the stub of your model on this line:

stub(model).error_messages { messages }

I presume that you actually want to do something else here, most likely:

model.should_receive(:error_messages).and_return(messages)

which creates a stub method for error_messages and will respond with your messages array whenever your spec tests calls model.error_messages