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

The Archive Base Latest Questions

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

I setup a mock object and told it to expect a check for nil

  • 0

I setup a mock object and told it to expect a check for nil and to return false:

status = double('status')
status.should_receive(:nil?).and_return(false)

I only expect the call to nil? to occur once, but I got an error in my rspec test, saying that status received nil? twice.

Is there a way to get rspec to show where/how each call occurred?

adding the ‘–backtrace’ option did not work.

  • 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-18T02:54:36+00:00Added an answer on June 18, 2026 at 2:54 am

    Try something like this:

    status.should_receive(:nil?).twice { puts caller; false }
    

    This tells rspec to allow two invocations and call the associated block each time. Thecaller method generates a full backtrace which you should be able to analyze onstdout. We also returnfalse to stay on the code-path we’re testing.

    If the two backtraces are hard to distinguish and you’re only interested in the second (unexpected) invocation, then set up two successive expectations:

    status.should_receive(:nil?).and_return(false)
    status.should_receive(:nil?) { puts caller; false }
    

    Here the double will return false on the first invocation and call the block on the second.

    Reference for setting responses on expectations:
    https://github.com/rspec/rspec-mocks#setting-responses

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

Sidebar

Related Questions

var mockedService = new Mock<IService>(); mockedService.Setup(x => x.InterfaceMethod(args)).Returns(value); _Service = mockedService.Object; MyController controller =new
setup.py from distutils.core import setup import py2exe setup(console=['program.py']) The error Traceback (most recent call
Setup Mac OSX 10.6.7, Eclipse, Tomcat 1.6 I am trying to call a string
I get a Moq object to return different values on successive calls to a
I have the following code: var connector = new Mock<IConector>(); connector .Setup(cn => cn.listar(FetchEstandar,
Which of these is correct? var mockLogger = new Mock<EntLibLogger>(); mockLogger.Setup(i => i.CreateTracer(It.IsAny<string>())) .Returns((string
So, I'm creating a mock object as a static variable on the class level
I am trying to Mock the MVC object HttpPostedFileWrapper so I can test the
I'm trying to setup an expected call to a protected method. The methods signature
[TestMethod] public void TestMethod1() { var mock = new Mock<EmailService>(); mock.Setup(x => x.SendEmail()).Returns(true); var

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.