views:

157

answers:

1

I am a fan of using mocks and stubs everywhere I can to keep my specs running quickly. I'm kind of stumped as to how I might do this to test the find_special method in the following:

  has_many :foos do 
    def find_special
      if proxy_owner.baz
        ... find stuff
      else
        ... find other stuff
      end
    end
  end

I wouldn't mind using the :extend => module syntax for this, but I don't think it makes a difference.

+1  A: 

Are you asking how to stub a method on the proxy_owner? In this case isn't it the object which you're calling foos on?

# in Mocha
item.stubs(:baz).returns(true)
item.foos.find_special # => find stuff
item.stubs(:baz).returns(false)
item.foos.find_special # => find other stuff

This is untested but maybe it will get you close.

ryanb
But I would still need to make a bunch of Foo objects and save them to the database before this test, right? I'm looking for a way where I could test that the find_special method causes Foo.find to be called with the right parameters, without actually needing to run the query against the database.
Matt Van Horn
Shorter version: I know how to test this with factories/fixtures - I want to see if I can do it without them.
Matt Van Horn
You can build item and foos in memory: Item.new and item.foos.build. No need to touch the database.
ryanb