Topic: Unused mocks in rspec failing tests

I'm using rspec and mocha for doing some tests. I've discovered some behaviour that is preventing me from tidying up the code.

I want to stub out the find_by_name for a particular activerecord model when supplied with a particular name. Quite a few of the tests do this and I found I was repeating the code all over the place. As a solution I moved it to the "before :all" block like so

before :all do
  PaymentType.stubs(:find_by_name).with("PayPal").returns( mock(:id => 1))

The problem is that if the method is not called it causes the test to fail

not all expectations were satisfied
unsatisfied expectations:
- expected exactly once, not yet invoked: #<Mock:0x5d7e9e8>.id(any_parameters)
satisfied expectations:
- allowed any number of times, not yet invoked: PaymentType(id: integer, name: string, created_at: datetime, updated_at: datetime).find_by_name('PayPal')

I'm not concerned if it's called or not. I just want that method to behave consistently across the tests without resorting to fixtures or repeating myself all the time. Is that possible? Even if you have some links that would shed light on this behaviour it would be greatly appreciated.

Last edited by StevenC (2010-06-18 11:30:29)