title: Rhino Mock calling an async Task Method
body: <p>I am trying to mock a dbWriteService Method that returns a aggregate exception when i call it, even though everything is not null. I am farely new to mocking and rhino mock, so I do not really get the problem.</p>
  
  <p>This is the part I want to mock in the function i want to test:</p>
  
  <pre><code>public async Task&lt;bool&gt; SaveDataAsync(object data) {
  ...
      await _dbWriteService.UpdateAsync(data);
  ...
  }
  </code></pre>
  
  <p>I am mocking the writeService like this:</p>
  
  <pre><code>dbWriteService = MockRepository.GenerateStub&lt;IDbWriteService&gt;();
  dbWriteService.Expect(service =&gt; service.UpdateAsync(null));
  var wasSaved = subject.SaveDataAsync(data).Result;
  dbWriteService.AssertWasCalled(service =&gt; service.UpdateAsync(null));
  </code></pre>
  
  <p>and i am getting an exception like this:</p>
  
  <pre><code>System.AggregateException: One or more errors occurred. ---&gt; System.NullReferenceException: Object reference not set to an instance of an object.
      at ClearingDataRepository.&lt;SaveDataAsync&gt;d__28.MoveNext() in ....\ClearingDataRepository.cs:line 170
   --- End of inner exception stack trace ---
      at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
     at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
     at System.Threading.Tasks.Task`1.get_Result()
     at UnitTests.Services.ClearingDataRepositoryTests.SaveDataAsync_Sucess() in ....\ClearingDataRepositoryTests.cs:line 90
  </code></pre>
  
  <p>I tried a few things with the mocked function but i do not get the reason why this stuff does not work. Could anybody explain this to me?</p>