Topic: having trouble testing an update action using rspec and mocha

Hi all,

got a bit of an issue that I cant seem to get my head around,

I'm trying to test an update action theres nothing special going on in the controller just the usual update _attributes etc. etc.

here is the spec I have written for testing it,

   # UPDATE
  describe "PUT actions/:id" do
    describe "with valid params" do

      before(:each) do
        @action = mock_model(Action, :update_attributes => true)
        Action.stubs(:find).with("1").and_return(@action)
      end

      it "should find action and return object" do
        Action.should_receive(:find).with("1").and_return(@action)
      end

    end

    describe "with in valid params" do

    end

and here is the failure message I keep getting:

Mocha::ExpectationError in 'ActionsController PUT actions/:id with valid params should find action and return object'
unexpected invocation: #<Mock:Action_1001>.__mock_proxy()
unsatisfied expectations:
- expected exactly once, not yet invoked: #<Mock:Action_1001>.destroyed?(any_parameters)
- expected exactly once, not yet invoked: #<Mock:Action_1001>.id(any_parameters)
- expected exactly once, not yet invoked: #<Mock:Action_1001>.errors(any_parameters)
- expected exactly once, not yet invoked: #<Mock:Action_1001>.update_attributes(any_parameters)
- expected exactly once, not yet invoked: #<Mock:Action_1001>.new_record?(any_parameters)
- expected exactly once, not yet invoked: #<Mock:Action_1001>.to_param(any_parameters)
satisfied expectations:
- allowed any number of times, not yet invoked: #<Mock:errors>.count(any_parameters)

can any help me understand whats wrong here and how to get back on the right track?

TIA

Yorkie

Re: having trouble testing an update action using rspec and mocha

yorkie-t wrote:
      before(:each) do
        @action = mock_model(Action, :update_attributes => true)
        Action.stubs(:find).with("1").and_return(@action)
      end

Whenever you mock an object you must stub out EVERY method that is called upon that mock object. The error message is a list  of all the methods being called on the object in that test. If you want it to pass you will need to stub out each of the methods listed in the error page.

An alternative option is to not use mocking in this instance. There is a guide about this, can't remember the url but the title is "Mocks aren't stubs". Have a google