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

  • SEARCH
  • Home
  • 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 7195073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:30:18+00:00 2026-05-28T20:30:18+00:00

Almost every spec file I come accross I end up writing stuff like: before

  • 0

Almost every spec file I come accross I end up writing stuff like:

  before :each do
    @cimg = Factory.build :cimg_valid
    @cimg.stub(:validate_img).and_return true
    @cimg.stub(:validate_img_url).and_return true
    @cimg.stub(:save_images).and_return true
    @cimg.stub(:process_image).and_return true
    @cimg.stub(:img).and_return true
  end

I mean, the model I get from Factory.build is completely valid. But if I don’t stub that stuff it saves things in the filesystem, and validates stuff I’m not testing…

What I mean, I think it would be cleaner to do something like this:

  before :each do
    @cimg = Factory.build :cimg_for_testing_tags
  end

If stubbing within the Factory is even possible.

What is the proper way to stub the model?

  • 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. Editorial Team
    Editorial Team
    2026-05-28T20:30:19+00:00Added an answer on May 28, 2026 at 8:30 pm

    In recent versions of factory_girl you have an after_build callback, so I believe you could define your factory like this:

    FactoryGirl.define do
      factory :cimg_for_testing_tags do
    
        ... # Factory attributes
    
        after_build do |cimg|
          cimg.stub(:validate_img).and_return true
        end
      end
    end
    

    UPDATE

    After factory_girl 3.3.0, the syntax has changed to following:

    FactoryGirl.define do
      factory :cimg_for_testing_tags do
    
        ... # Factory attributes
    
        after(:build) do |cimg|
          cimg.stub(:validate_img).and_return true
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Almost every project that I work will have some sort of tab delimited file
I have a large file system in which almost every folder has a file
I have a file in a Subversion repository which changes almost every time it's
It appears that on CDMA Blackberry 8330, almost every mp3 file I'm trying to
Almost every time I run my MVC app, it stops with errors before getting
I am writing an email application using PyGTK. In almost every message that I
In my code almost every function has one or more malloc calls, and each
I have a large codebase, where almost every single file isn't aligned properly, has
As almost every programmer, I'm writing my own PHP framework for educational purposes. And
Almost every Java book I read talks about using the interface as a way

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.