views:

76

answers:

0

Today I ran into an issue using RoR to stub calls to AR objects. I thought that I'd be able to do something along the lines of :

stub.instance_of(BankAccount).save_to_other_spot { true }

However when I tried this method it didn't seem to stub the method at all and it would end up running the original method I was trying to stub. I confirmed this using debugger etc.

So I ended up using the following method :

stub.proxy(BankAccount).find(anything) do |account|
  stub(account).save_to_other_spot { true }
  account
end

This works.

I was wondering if I'm doing something wrong though? Why doesn't instance_of work in the way I expect?

Another issue I ran into was that in my RSpec tests I seem to have to setup my mocks and stubs for each request. Again, is this normal or am I doing something wrong?

By this I mean I'd have to do something like :

... mock and stub ...
get :show, :id => @id

... mock and stub ...
post :update, :id => id, :account => { ... params ... }

I thought I'd be able to mock and stub once at the top.