Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 151225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:24:57+00:00 2026-05-11T09:24:57+00:00

I am using Mocha and Factory_girl in a JRuby rails application. When I call

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:24:58+00:00Added an answer on May 11, 2026 at 9:24 am

    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 a Factory and not an instance of the object you want to construct (the tweet_feed_with_tweets, in your example). This means that setting an expectation for the pull_tweets method on t is setting the expectation on the Factory instance and not on the object that will be constructed when you call Factory(: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.define block. 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 :-

    def test_should_do_something   tweet_feed = Factory(:tweet_feed)   tweet_feed.expects(:pull_tweets).returns([Factory.build(:status), Factory.build(:status)])   # test stuff here end 

    If you need this in multiple places, you can extract it into a method :-

    def test_should_do_something   tweet_feed = build_tweet_feed_with_tweets   # test stuff here end  private  def build_tweet_feed_with_tweets   tweet_feed = Factory(:tweet_feed)   tweet_feed.expects(:pull_tweets).returns([Factory.build(:status), Factory.build(:status)])   return tweet_feed end 

    A couple of other thoughts :-

    1. I think setting expectations in such a hidden away place is probably a bad idea anyway.
    2. If you are going to do it, I would have thought using stubs was more appropriate than expects.
    3. It might be worth separating the pull_tweets method (and any similar methods) into a TwitterAPI class. That way it wouldn’t seem so bad that you need to set up an expectation on the TwitterAPI in the test.

    I hope some of that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
Using PHP, I'd like to convert a string containing a Roman number into its
using ... $this->getInvokeArg('bootstrap')->getOptions(); .. to retrieve my config settings from application.ini (using Zend Framework
Using CouchDB 1.0.1. I have DELETEd some documents, then I PUT some other documents
Using Foursquare API "Venue" service.. I am parsing nearByVenue details like shop, restaurant etc.
Using online interfaces to a version control system is a nice way to have
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using TortoiseSVN against VisualSVN I delete a source file that I should not have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.