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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:33:27+00:00 2026-05-16T21:33:27+00:00

we have a weired problem when using Rhino Mocks and Threads. I’ve tried to

  • 0

we have a weired problem when using Rhino Mocks and Threads. I’ve
tried to isolate the problem, but now I’m stuck to this:

[TestClass]
public class FoolTests
{
   [TestMethod]
   public void TestMethod_Scenario_Result()
   {
       for (int i = 0; i < 5; i++)
       {
           var fool = MockRepository.GenerateStub<IFool>();
           fool.Stub(x => x.AmIFool).Return(false);
           new Fool(fool);
       }
   }
}

public class Fool
{
   private readonly IFool _fool;
   private readonly Thread _thread;

   public Fool(IFool fool)
   {
       _fool = fool;
       _thread = new Thread(Foolish);
       _thread.Start();
   }

   private void Foolish()
   {
       while (true)
       {
           var foo = _fool.Foolness;
       }
   }
}

public interface IFool
{
   bool AmIFool { get; }
   bool Foolness { get; set; }
}

Nearly all the time when running this test, I get “Test method FoolTests.TestMethod_Scenario_Result threw exception: System.InvalidOperationException: This action is invalid when the mock object is in replay state.” on line “fool.Stub(x => x.AmIFool).Return(false);”.

I have no idea what should be wrong here. Has anyone an idea or do I have to dig into the Rhino Mocks-code?

  • 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-16T21:33:28+00:00Added an answer on May 16, 2026 at 9:33 pm

    Not sure if this is a complete answer, but this page has an interesting note on multi-threading:

    http://www.ayende.com/projects/rhino-mocks/api/files/MockRepository-cs.html

    MockRepository is capable of verifying in multiply threads, but recording in multiply threads is not recommended.

    First thing I’d try is setting up all mocks and expectations, then executing your constructors. This seems to be working for me:

    [TestMethod]
    public void TestMethod_Scenario_Result()
    {
        var stubs = new IFool[5];
        for (int i = 0; i < stubs.Length; ++i)
        {
            var fool = MockRepository.GenerateStub<IFool>();
            fool.Stub(x => x.AmIFool).Return(false);
            stubs[i] = fool;
        }
    
        foreach (var stub in stubs)
            new Fool(stub);
    }
    

    Since this code works, I imagine the problem is that you are doing playback in one thread while recording (for a different stub) in another thread. Playing back and recording at the same time seems like it isn’t thread safe, even if you’re operating on different stubs.

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

Sidebar

Related Questions

I have come across quite a wired problem right now. I am using ASP.NET
Weired problem! ASP.NET Session expires instantly. In my web.config I have this session settings:
I have a weird problem. I create this window, but its blank until i
I know how to use RVM, but now I have a weird problem, which
I have a very weird problem. This ONLY affects Firefox on Mac, using the
We have this really weird problem in a Web Application when using ActiveRecord 2.0
I have a weird problem. I'm using pscp.exe from within a C# program (with
I'm fairly new to using MSSQL and have run into a weird problem. Given
I'm using VS10 and C# and I've encountered a weird problem. I have 2
I have a weired problem. One of the pages in my application show's status

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.