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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:21:24+00:00 2026-06-13T17:21:24+00:00

I have the following code. I have a somewhat legitimate reason for stubbing that

  • 0

I have the following code. I have a somewhat legitimate reason for stubbing that property twice (See explanation below). It looks like it’s only letting me stub it once.

private IStatus _status;

[SetUp()]
public void Setup() {
  this._status = MockRepository.GenerateStub<IStatus>();
  this._status.Stub(x => x.Connected()).Return(true);
  // This next line would usually be in the Setup for a subclass
  this._status.Stub(x => x.Connected()).Return(false);
}

[Test()]
public void TestTheTestFramework() {
  Assert.IsFalse(this._status.Connected()); // Fails...
}

public interface IStatus {
  bool Connected { get; }
}

I tried downloading the most recent build (3.6 build 21), but still have the same issue. Any ideas on why I can’t do this? I tried changing the Connected property on IStatus to be a function and the test still failed. I get the same behavior in VB.Net… Bug?

Explanation on the double-stubbing

I’m structuring my tests around inheritance. That way I can do common setup code just once, using injected mocked dependencies to simulate different conditions. I might provide a base/default stubbed value (e.g. yes, we’re connected) which I’d want to override in the subclass that tests the behavior of the SUT when the connection is down. I usually end up with code like this.

[TestFixture()]
public class WhenPublishingAMessage {

  // Common setup, inject SUT with mocked dependencies, etc...

  [Test()]
  public void ShouldAlwaysWriteLogMessage {
    //Example of test that would pass for any sub-condition
  }

  [TestFixture()]
  public class AndNoConnection : WhenPublishingAMessage {
    // Do any additional setup, stub dependencies to simulate no connection
    // Run tests for this condition
  }

  [TestFixture()]
  public class AndHaveConnection : WhenPublishingAMessage {
    // Do any additional setup and run tests for this condition
  }
}

Edit

This post on the Rhino Mocks google group might be helpful. It looks like I might need to call this._status.BackToRecord(); to reset the state, so to speak… also, tacking on .Repeat.Any() to the second stub statement seemed to help as well. I’ll have to post more details later.

  • 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-13T17:21:26+00:00Added an answer on June 13, 2026 at 5:21 pm

    To sum everything up, there’s a three different answers that are possible:

    Tallmaris’s answer:

    Specify a specific number of times to return on the first stub using .Repeat.Times(n), .Repeat.Once(), .Repeat.Twice(), etc. For example:

    this._status.Stub(x => x.Connected()).Return(true).Repeat.Once();
    this._status.Stub(x => x.Connected()).Return(false);

    This method works pretty well if I know the number of times the stub will get called before I change it’s behavior (e.g. it just gets called once in the constructor).

    Reset the mocked object

    I don’t like this method since I’d like to avoid the (at least to me) more cumbersome Expect/Verify Record/Replay type syntax. It was recommending to me in response to a post I made to the Rhino Mocks Google group with the same title as this question.

    this._status.Stub(x => x.Connected).Return(true);
    
    this._status.GetMockRepository().BackToRecordAll();
    this._status.GetMockRepository().ReplayAll();
    
    this._status.Stub(x => x.Connected).Return(false);
    

    Using the magical Repeat.Any

    I found that using .Repeat.Any() on the second stub overrode the first one work… I feel a bit bad adding some extra ‘magic’ code to make it work, but in the case where you don’t know how often to tell the first stub to return, this option will work.

    this._status.Stub(x => x.Connected()).Return(true);
    this._status.Stub(x => x.Connected()).Return(false).Repeat.Any();
    

    Note: you can’t do .Repeat.Any() more than once.

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

Sidebar

Related Questions

I have a piece of Perl code somewhat like the following (strongly simplified): There
I have xml that looks somewhat similar to the following. I need to get
I have following code that I am compiling in a .NET 4.0 project namespace
I am somewhat losing my mind here... I have the following code: <script id=myTemplate
I have a javascript file which has some code somewhat like this: var Slide
I've come across a proprietary stack-based scripting language that looks like a somewhat simplified
I have some Java code which sends out an email with code somewhat like
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig
I have following code <div id=main> <div id=one> </div> <div id=two> </div> <div id=three>
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using

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.