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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:50:42+00:00 2026-05-18T03:50:42+00:00

I’m currently building a class using TDD. The class is responsible for waiting for

  • 0

I’m currently building a class using TDD. The class is responsible for waiting for a specific window to become active, and then firing some method.

I’m using the AutoIt COM library (for more information about AutoIt look here) since the behavior I want is actually a single method in AutoIt.

The code is pretty much as the following:

public class WindowMonitor
{
    private readonly IAutoItX3 _autoItLib;

    public WindowMonitor(IAutoItX3 autoItLib)
    {
        _autoItLib = autoItLib;
    }


    public void Run() // indefinitely
    {
        while(true)
        {
            _autoItLib.WinWaitActive("Open File", "", 0);
            // Do stuff now that the window named "Open File" is finally active.
        }
    }
}

As you can see the AutoIt COM library implements an interface wich I can mock (Using NUnit and Rhino Mocks):

[TestFixture]
 public class When_running_the_monitor
 {
  WindowMonitor subject;
  IAutoItX3 mockAutoItLibrary;
  AutoResetEvent continueWinWaitActive;
  AutoResetEvent winWaitActiveIsCalled;


 [SetUp]
 public void Setup()
  {
   // Arrange
   mockAutoItLibrary = MockRepository.GenerateStub<IAutoItX3>();
   mockAutoItLib.Stub(m => m.WinWaitActive("", "", 0))
                .IgnoreArguments()
                .Do((Func<string, string, int, int>) ((a, b, c) =>
                {
                    winWaitActiveIsCalled.Set();
                    continueWinWaitActive.WaitOne();
                    return 1;
                }));

   subject = new Subject(mockAutoItLibrary)

   // Act
   new Thread(new ThreadStart(subject.Run)).Start();
   winWaitActiveIsCalled.WaitOne();
  }

  // Assert

    [Test]
    [Timeout(1000)]
    public void should_call_winWaitActive()
    {
        mockAutoItLib.AssertWasCalled(m => m.WinWaitActive("Bestand selecteren", "", 0));
    }

    [Test]
    [Timeout(1000)]
    public void ensure_that_nothing_is_done_while_window_is_not_active_yet()
    {
        // When you do an "AssertWasCalled" for the actions when the window becomes active, put an equivalent "AssertWasNotCalled" here.

    }

}

The problem is, the first test keeps timing out. I have already found out that when the stub “WinWaitActive” is called, it blocks (as intended, on the seperate thread), and when the “AssertWasCalled” is called after that, execution never returns.

I’m at a loss how to proceed, and I couldn’t find any examples of mocking out a blocking call.

So in conclusion:

Is there a way to mock a blocking call without making the tests timeout?

(P.S. I’m less interested in changing the design (i.e. “Don’t use a blocking call”) since it may be possible to do that here, but I’m sure there are cases where it’s a lot harder to change the design, and I’m interested in the more general solution. But if it’s simply impossible to mock blocking calls, suggestions like that are more that welcome!)

  • 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-18T03:50:43+00:00Added an answer on May 18, 2026 at 3:50 am

    Not sure if I understand the problem.

    Your code is just calling a method on the mock (WinWaitActive). Of course, it can’t proceed before the call returns. This is in the nature of the programming language and nothing you need to test.

    So if you test that WinWaitActive gets called, your test is done. You could test if WinWaitActive gets called before anything else, but this requires ordered expectations, which requires the old style rhino mocks syntax and is usually not worth to do.

       mockAutoItLibrary = MockRepository.GenerateStub<IAutoItX3>();
    
       subject = new Subject(mockAutoItLibrary)
       subject.Run()
    
       mockAutoItLib.AssertWasCalled(m => m.WinWaitActive("Open File", "", 0));
    

    You don’t do anything else then calling a method … so there isn’t anything else to test.

    Edit: exit the infinite loop

    You could make it exit the infinite loop by throwing an exception from the mocks. This is not very nice, but it avoids having all this multi-threading stuff in the unit test.

       mockAutoItLibrary = MockRepository.GenerateStub<IAutoItX3>();
    
       // make loop throw an exception on second call
       // to exit the infinite loop
       mockAutoItLib
         .Stub(m => m.WinWaitActive(
           Arg<string>.Is.Anything, 
           Arg<string>.Is.Anything, 
           Arg<int>.Is.Anything));
         .Repeat.Once();
    
       mockAutoItLib
         .Stub(m => m.WinWaitActive(
           Arg<string>.Is.Anything, 
           Arg<string>.Is.Anything, 
           Arg<int>.Is.Anything));
         .Throw(new StopInfiniteLoopException());
    
       subject = new Subject(mockAutoItLibrary)
       try
       {
         subject.Run()
       }
       catch(StopInfiniteLoopException)
       {} // expected exception thrown by mock
    
       mockAutoItLib.AssertWasCalled(m => m.WinWaitActive("Open File", "", 0));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.