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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:34:51+00:00 2026-06-18T06:34:51+00:00

In writing Rspec tests, I’m often frustrated with should_receive . I’d like to know

  • 0

In writing Rspec tests, I’m often frustrated with should_receive. I’d like to know if there’s a less intrusive alternative.

For example:

describe "making a cake" do
  it "should use some other methods" do
    @baker.should_receive(:make_batter)
    @baker.make_cake
  end
end

The call to should_receive is a good description, but it breaks my code, because should_receive works by masking the original method, and make_cake can’t proceed unless make_batter actually returns some batter. So I change it to this:

@baker.should_receive(:make_batter).and_return(@batter)

This is ugly because:

  • It looks like I’m testing that make_batter correctly returns @batter, but I’m actually forcing the fake version of make_batter to return that.
  • It forces me to separately set up @batter
  • If make_batter has any important side effects (which could be a code smell, I suppose) I have to make those happen, too.

I wish that should_receive(:make_batter) would verify the method call and pass it on to the original method. If I wanted to stub its behavior for better isolation testing, I would do so explicitly: @baker.stub(:make_batter).and_return(@batter).

Is there a way to do something like should_receive without preventing the original method call? Is my problem a symptom of bad design?

  • 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-18T06:34:52+00:00Added an answer on June 18, 2026 at 6:34 am

    It looks like the nicer API to delegate to the original method that Myron Marston alluded to has actually been added in rspec-mocks v2.12.0

    • Add and_call_original which delegates to the original method.

    So now you can simply do this any time you “want to set a message expecation without interfering with how the object responds to the message”:

    @baker.should_receive(:make_batter).and_call_original
    

    Thanks for adding this, Myron.

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

Sidebar

Related Questions

I have been writing some rspec tests, and right now I want to verify
I've been writing rspec + capybara tests for my application and came up with
I'm writing RSpec tests and I have come to a point where I am
I just started writing RSpec tests, and I came across thoughtbot's Style Guide ,
So I'm writing some rspec tests and I'm embarrassed at my lack of Ruby
I'm trying to write tests for an engine gem I'm writing. I'm using Rspec
I am new to rails and have just started writing tests using rspec version
I'm writing some selenium tests (in rspec for a rails app). One of my
I'm writing a rspec tests for my controller and i cannot find solution following
I have started writing tests using Rspec for a really old project. The models

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.