Note that there are some explanatory texts on larger screens.

plurals
  1. POMocking a blocking call with Rhino Mocks
    primarykey
    data
    text
    <p>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.</p> <p>I'm using the AutoIt COM library (for more information about AutoIt look <a href="http://www.autoitscript.com/autoit3/index.shtml" rel="nofollow">here</a>) since the behavior I want is actually a single method in AutoIt.</p> <p>The code is pretty much as the following:</p> <pre><code>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. } } } </code></pre> <p>As you can see the AutoIt COM library implements an interface wich I can mock (Using NUnit and Rhino Mocks):</p> <pre><code>[TestFixture] public class When_running_the_monitor { WindowMonitor subject; IAutoItX3 mockAutoItLibrary; AutoResetEvent continueWinWaitActive; AutoResetEvent winWaitActiveIsCalled; [SetUp] public void Setup() { // Arrange mockAutoItLibrary = MockRepository.GenerateStub&lt;IAutoItX3&gt;(); mockAutoItLib.Stub(m =&gt; m.WinWaitActive("", "", 0)) .IgnoreArguments() .Do((Func&lt;string, string, int, int&gt;) ((a, b, c) =&gt; { 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 =&gt; 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. } </code></pre> <p>}</p> <p>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.</p> <p>I'm at a loss how to proceed, and I couldn't find any examples of mocking out a blocking call.</p> <p>So in conclusion:</p> <p>Is there a way to mock a blocking call without making the tests timeout?</p> <p>(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!)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload