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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:05:45+00:00 2026-05-13T08:05:45+00:00

In rspec (1.2.9), what is the correct way to specify that an object will

  • 0

In rspec (1.2.9), what is the correct way to specify that an object will receive multiple calls to a method with a different argument each time?

I ask because of this confusing result:

describe Object do

  it "passes, as expected" do
    foo = mock('foo')
    foo.should_receive(:bar).once.ordered.with(1)
    foo.should_receive(:bar).once.ordered.with(2)
    foo.bar(1)
    foo.bar(2)
  end

  it "fails, as expected" do
    foo = mock('foo')
    foo.should_receive(:bar).once.ordered.with(1) # => Mock "foo" expected :bar with (1) once, but received it twice
    foo.should_receive(:bar).once.ordered.with(2)
    foo.bar(1)
    foo.bar(1)
    foo.bar(2)
  end

  it "fails, as expected" do
    foo = mock('foo')
    foo.should_receive(:bar).once.ordered.with(1)
    foo.should_receive(:bar).once.ordered.with(2)
    foo.bar(2) # => Mock "foo" received :bar out of order
    foo.bar(1)
  end

  it "fails, as expected, but with an unexpected message" do
    foo = mock('foo')
    foo.should_receive(:bar).once.ordered.with(1)
    foo.should_receive(:bar).once.ordered.with(2)
    foo.bar(1)
    foo.bar(999) # => Mock "foo" received :bar with unexpected arguments
                 # =>   expected: (1)
                 # =>         got (999)
  end

end

I expected the last failure message to be “expected: (2)”, not “expected (1)”. Have I used rspec incorrectly?

  • 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-13T08:05:46+00:00Added an answer on May 13, 2026 at 8:05 am

    Similar to this question. The recommended solution is to call as_null_object to avoid the confusion of messages. So:

    describe Object do
      it "fails, as expected, (using null object)" do
        foo = mock('foo').as_null_object
        foo.should_receive(:bar).once.ordered.with(1)
        foo.should_receive(:bar).once.ordered.with(2)
        foo.bar(1)
        foo.bar(999) # => Mock "foo" expected :bar with (2) once, but received it 0 times
      end
    end
    

    The output is not the same as your second case (i.e. “expected 2 but got 999”), but it does show that the expectation was not met.

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

Sidebar

Related Questions

I'm writing an rspec scenario thats failing with: (#<User:0x1056904f0>).update_attributes(#<RSpec::Mocks::ArgumentMatchers::AnyArgMatcher:0x105623648>) expected: 1 time received: 0
I love the Ruby RSpec BDD development style. Are there any good tools for
I want to use RSpec mocks to provide canned input to a block. Ruby:
When dropping the use of rspec and rspec-rails plugins and switching to the gem
I am trying to write an interface between RSPEC (ruby flavoured BDD) and a
When should I use specs for Rails application and when Cucumber (former rspec-stories)? I
After doing some work with Ruby, Rails, and RSpec last summer and I learned
I'm pretty confused how to validate boolean values in Rspec and Rails. I understand
I'm following the rails tutorial here: http://railstutorial.org/chapters/filling-in-the-layout#top When I run rspec spec/, I get
Through rspec (I'm using rspec-1.3.0, rspec-rails-1.3.2 gems) generator ( ruby script/generate rspec_model suggestion section_id:integer

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.