title: Unit testing with FromAsyncPattern
body: <p>The Reactive Extensions have a sexy little hook to simplify calling async methods:</p>
  
  <pre><code>var func = Observable.FromAsyncPattern&lt;InType, OutType&gt;(
      myWcfService.BeginDoStuff,
      myWcfService.EndDoStuff);
  
  func(inData).ObserveOnDispatcher().Subscribe(x =&gt; Foo(x));
  </code></pre>
  
  <p>I am using this in an WPF project, and it works great at runtime.  </p>
  
  <p>Unfortunately, when trying to unit test methods that use this technique I am experiencing random failures.  ~3 out of every five executions of a test that contain this code fails.</p>
  
  <p>Here is a sample test (implemented using a Rhino/unity auto-mocking container):</p>
  
  <pre><code>[TestMethod()]
  public void SomeTest()
  {
     // arrange
     var container = GetAutoMockingContainer();
  
     container.Resolve&lt;IMyWcfServiceClient&gt;()
        .Expect(x =&gt; x.BeginDoStuff(null, null, null))
        .IgnoreArguments()
        .Do(
           new Func&lt;Specification, AsyncCallback, object, IAsyncResult&gt;((inData, asyncCallback, state) =&gt;
              {
                 return new CompletedAsyncResult(asyncCallback, state);
               }));
  
     container.Resolve&lt;IRepositoryServiceClient&gt;()
        .Expect(x =&gt; x.EndDoStuff(null))
        .IgnoreArguments()
        .Do(
           new Func&lt;IAsyncResult, OutData&gt;((ar) =&gt;
           {
              return someMockData;
           }));
  
     // act
     var target = CreateTestSubject(container);
  
     target.DoMethodThatInvokesService();
  
     // Run the dispatcher for everything over background priority
     Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new Action(() =&gt; { }));
  
     // assert
     Assert.IsTrue(my operation ran as expected);
  }
  </code></pre>
  
  <p>The problem that I see is that the code that I specified to run when the async action completed (in this case, Foo(x)), is never called.  I can verify this by setting breakpoints in Foo and observing that they are never reached.  Further, I can force a long delay after calling DoMethodThatInvokesService (which kicks off the async call), and the code is still never run.  I <strong>do</strong> know that the lines of code invoking the Rx framework were called.</p>
  
  <p>Other things I've tried:</p>
  
  <ul>
  <li><p>I have attempted to modify the second last line according to the suggestions here:  <a href="https://stackoverflow.com/questions/2184260/reactive-extensions-rx-unit-testing-something-with-observeondispatcher">Reactive Extensions Rx - unit testing something with ObserveOnDispatcher</a>  No love.</p></li>
  <li><p>I have added <code>.Take(1)</code> to the Rx code as follows:</p>
  
  <p>func(inData).ObserveOnDispatcher().Take(1).Subscribe(x => Foo(x));</p></li>
  </ul>
  
  <p>This improved my failure rate to something like 1 in 5, but they still occurred.</p>
  
  <ul>
  <li>I have rewritten the Rx code to use the plain jane Async pattern.  This works, however my developer ego really would love to use Rx instead of boring old begin/end.</li>
  </ul>
  
  <p>In the end I do have a work around in hand (i.e. don't use Rx), however I feel that it is not ideal.  If anyone has ran into this problem in the past and found a solution, I'd dearly love to hear it.</p>
  
  <p><strong>Update</strong>:</p>
  
  <p>I also <a href="http://social.msdn.microsoft.com/Forums/en-US/rx/thread/d571a401-3a20-473e-ae4e-32fc698cd11b" rel="nofollow noreferrer">posted on the Rx forums</a>, and they will be including a test scheduler with a coming release.  That will probably be the ultimate solution once it is available.</p>