title: How to Mock a Task<> Result?
body: <p>I'm setting up some unit tests and using Rhino Mocks to populate the object being tested.  One of the things being mocked is a <code>Task&lt;HttpResponseMessage&gt;</code>, since the logic being tested includes a call to an <code>HttpClient</code> to get an async response.</p>
  
  <p>So I've started setting up the mocks like this:</p>
  
  <pre><code>var httpClient = MockRepository.GenerateMock&lt;HttpClient&gt;();
  var taskFunc = MockRepository.GenerateMock&lt;Func&lt;HttpResponseMessage&gt;&gt;();
  var responseTask = MockRepository.GenerateMock&lt;Task&lt;HttpResponseMessage&gt;&gt;(taskFunc);
  var response = MockRepository.GenerateMock&lt;HttpResponseMessage&gt;();
  
  httpClient.Stub(c =&gt; c.PostAsJsonAsync&lt;IEnumerable&lt;LogMessage&gt;&gt;(Arg&lt;string&gt;.Is.Anything, Arg&lt;IEnumerable&lt;LogMessage&gt;&gt;.Is.Anything)).Return(responseTask);
  responseTask.Stub(t =&gt; t.Result).Return(response);
  response.Stub(r =&gt; r.IsSuccessStatusCode).Return(true);
  </code></pre>
  
  <p>(The "act" step of the test will be to instantiate the object being tested, feed it the <code>httpClient</code>, and run a method on it.  The "assert" will verify via the mocks that expected method calls were made on them.)</p>
  
  <p>Stepping through this in a debugger, there's an indefinite hang on this line:</p>
  
  <pre><code>responseTask.Stub(t =&gt; t.Result).Return(response);
  </code></pre>
  
  <p>I don't have a lot of experience with Rhino Mocks or with C# async, so I may be overlooking something obvious.  The goal, of course, is that any call to the <code>.Result</code> property would return the <code>response</code> mock.  But it looks like my attempt itself is perhaps invoking <code>.Result</code> which I would expect to wait indefinitely since it's just a mock, perhaps?</p>
  
  <p>What is the right way to arrange this?  Essentially I need to supply my object with a mocked <code>HttpClient</code> and assert that a method was called on it with a specific argument.</p>