I am using Mocha and Factory_girl in a JRuby rails application. When I call the factory I would like to return the objects with some mocking already done. Here is a code snippet of what I am trying to do.
Factory.define :tweet_feed_with_tweets, :parent => :tweet_feed do |t| t.expects(:pull_tweets).returns([Factory.build(:status),Factory.build(:status)]) end
Because I do not want my unit and functional test to actually pull from the twitter API i want to stub the method so it returns what I want. But, this is not working. The object comes back without any stubbing done. Is there a way to actually have stubbing performed on an object created with factory girl before it gets returned to you?
Looking at the documentation & source code for
factory_girl, it looks like the object yielded to the block (t, in your example) is an instance of aFactoryand not an instance of the object you want to construct (thetweet_feed_with_tweets, in your example). This means that setting an expectation for thepull_tweetsmethod ontis setting the expectation on theFactoryinstance and not on the object that will be constructed when you callFactory(:tweet_feed_with_tweets). I think this explains why your example is not working as you expect.I may be wrong, but I can’t see a way of adding the expectation within the
Factory.defineblock. You’ve probably already thought of this, but I think you’d be better off adding the expectation in the test after you’ve constructed the instance :-If you need this in multiple places, you can extract it into a method :-
A couple of other thoughts :-
stubswas more appropriate thanexpects.pull_tweetsmethod (and any similar methods) into aTwitterAPIclass. That way it wouldn’t seem so bad that you need to set up an expectation on theTwitterAPIin the test.I hope some of that helps.