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 7956225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:55:47+00:00 2026-06-04T03:55:47+00:00

I’ve got the following model class User < ActiveRecord::Base before_create :set_some_values private def set_some_values

  • 0

I’ve got the following model

class User < ActiveRecord::Base
  before_create :set_some_values

  private
  def set_some_values
    #do something
  end
end

In specs I’m using Fabrication gem to create objects but I can’t find a way to stub the set_some_values method. I tried

User.any_instance.stub!(:set_some_values).and_return(nil)

but Fabrication seems to ignore this. Is it possible to do?

  • 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-06-04T03:55:48+00:00Added an answer on June 4, 2026 at 3:55 am

    This is why I don’t like ActiveRecord callbacks — because if you want to have nothing to do with a callback (because, say, you’re making a call to an external service inside the callback) you still have to be concerned about stubbing it out. Yes you could stub out methods inside the callback, but it’s the same problem, and actually it’s a bit worse because now you are concerned about something inside a method you want nothing to do with.

    As usual there are multiple options here.

    One option which I’ve used a lot in the past is, add a condition to your callback that turns it off by default. So your Post class could look like:

    class Post
      before_save :sync_with_store, :if => :syncing_with_store?
    
      def syncing_with_store?; @syncing_with_store; end
      attr_writer :syncing_with_store
    
      def sync_with_store
         # make an HTTP request or something
      end
    end
    

    Now wherever you really want to call the callback (perhaps in your controller or wherever), you can set post.syncing_with_store = true before you call post.save.

    The downside to this approach is, it’s something that you (and other devs working with you) have to keep in mind, and it’s not really obvious that you have to do this. On the other hand, if you forget to do this, nothing bad happens.

    Another option is to use a fake class. Say you have a Post that pushes its data to an external data store on save. You could extract the code that does the pushing to a separate class (e.g. Pusher) which would be accessible at Post.pusher_service. By default, though, this would be set to a fake Pusher class that responds to the same interface but does nothing. So like:

    class Post
      class << self
        attr_accessor :pusher_service
      end
      self.pusher_service = FakePostPusher
    
      before_save :sync_with_store
    
      def sync_with_store
        self.class.pusher_service.run(self)
      end
    end
    
    class FakePostPusher
      def self.run(post)
        new(post).run
      end
    
      def initialize(post)
        @post = post
      end
    
      def run
        # do nothing
      end
    end
    
    class PostPusher < FakePostPusher
      def run
        # actually make the HTTP request or whatever
      end
    end
    

    In your production environment file, you’d set Post.pusher_service = Pusher. In individual tests or test cases, you’d make a subclass of Post — let(:klass) { Class.new(Post) } — and set klass.pusher_service = Pusher (that way you don’t permanently set it and affect future tests).

    The third approach, which I have been experimenting with, is this: simply don’t use ActiveRecord callbacks. This is something I picked up from Gary Bernhardt’s screencasts (which, by the way, are pretty amazing). Instead, define a service class that wraps the act of creating a post. Something like:

    class PostCreator
      def self.run(attrs={})
        new(attrs).run
      end
    
      def initialize(attrs={})
        @post = Post.new(attrs)
      end
    
      def run
        if @post.save
          make_http_request
          return true
        else
          return false
        end
      end
    
      def make_http_request
        # ...
      end
    end
    

    This way PostCreator.run(attrs) is the de facto way of creating a post instead of going through Post. Now to test saves within Post, there’s no need to stub out callbacks. If you want to test the PostCreator process, there’s no magic going on, you can easily stub out whichever methods you want or test them independently. (You could argue that stubbing out methods here is the same as stubbing out AR callbacks, but I think it’s more explicit what’s going on.) Obviously this only handles post creation, but you could do the same for post updating too.

    Anyway, different ideas, pick your poison.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
i got an object with contents of html markup in it, for example: string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and

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.