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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:53:06+00:00 2026-05-31T05:53:06+00:00

I’m using rspec and the rspec mocks library for stubbing/mocking, but I’m starting to

  • 0

I’m using rspec and the rspec mocks library for stubbing/mocking, but I’m starting to think that the line between stubbing out a method and asserting how it’s been used is blurred. In my tests I’ll often write something like:

before(:each) do
  subject.should_receive(:sum).once.with([1, 2, 3]).and_return(6)
end

it("should do something that relies on sum") do
  subject.call_something([1, 2, 3]).should == 24
end

But what are my assertions? That call_something([1, 2, 3]):

  • Returns 24
  • Calls sum([1, 2, 3]) during it’s execution

However, there is only one it block – the other assertion is hidden away in the stub definition. To put it another way, my stub is also my assertion. Wouldn’t it be much clearer to separate the two, and put an explicit assertion in for how the stubbed method was called:

before(:each) do
  # set up what my stub should return for a given input
  subject.may_receive(:sum).with([1, 2, 3]).and_return(6)
end

# assert how my stub was actually called
it("should have called sum with 1, 2, 3") do
  # this is pseudo-rspec
  subject.call_something([1, 2, 3]).should have_called(:sum).on(subject).once.with([1, 2, 3])
end
it("should do something that relies on sum") do
  subject.call_something([1, 2, 3]).should == 24
end

This way it’s very clear what I’m asserting because my stub definitions and assertions have been separated. I can setup my test at the top, and then check it’s behaviour at the bottom, without mixing the two.

So, my question is whether there’s a way of doing this? Most mocking frameworks work in the same way as rspec-mocks, and define the contract for how a stub method is used along with the expected behaviour, and then automagically check the assertion at the end.

My point is a conceptual one about how BDD works, and might be a bit subtle. Let me know I need to clarify it further!

  • 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-31T05:53:07+00:00Added an answer on May 31, 2026 at 5:53 am

    You’re correct. should_receive is an assertion (more accurately it is an expectation) and should not go in a before block. If you need the stubbing side-effect of should_receive, then stub separately in your before:

    before do
      subject.stub(:sum).and_return(6)
    end
    
    it "should do something that relies on sum" do
      subject.should_receive(:sum).once.with([1, 2, 3]).and_return(6)
      subject.call_something([1, 2, 3]).should == 24
    end
    

    This keeps the stubbing and expectation separate and where they belong.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... 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.