title: Testing an async method in ASP MVC Controller with Nunit
body: <p>I have an ASP.NET MVC application, with an Controller that features asynchronous methods, returning <code>Task&lt;PartialViewResult&gt;</code> object and marked with the async keyword.
  This method only takes the data from the database in async mode.</p>
  
  <pre><code>public async Task&lt;PartialViewResult&gt; SomeMethod()
  {
      using (var unitOfWork = _factory.Create())
      {
          var result = await unitOfWork.SomeRepository.GetAsync();
  
          return PartialView(result);
      };
  }
  </code></pre>
  
  <p>During testing, the stream just freeze in this spot (At run time this code works well):</p>
  
  <pre><code>var models = await unitOfWork.SomeRepository.GetAsync();
  </code></pre>
  
  <p>This is my test for this method:</p>
  
  <pre><code>public void GetExchange_GetView_OkModelIsViewModel()
  { 
      //fake Repository returns fake Data from DB
      var mockSomeRepository = new Mock&lt;ISomeRepository&gt;();
      mockSomeRepository.Setup(x =&gt; x.GetAsync(...).Returns(new Task&lt;List&lt;SomeType&gt;&gt;(() =&gt; new List&lt;SomeType&gt;()));
  
      //fake UoW returns fake Repository
      var mockUnitOfWork = new Mock&lt;IUnitOfWork&gt;();
      mockUnitOfWork.Setup(x =&gt; x.SomeRepository).Returns(mockSomeRepository.Object);
  
      //fake factory create fake UoW
      var fakeUnitOfWorkFactory = new Mock&lt;UnitOfWorkFactory&gt;();
      fakeUnitOfWorkFactory.Setup(x =&gt; x.Create()).Returns(mockUnitOfWork.Object);
  
      //Our controller
      var controller = new SomeController(fakeUnitOfWorkFactory);
  
      //Our async method
      var result = controller.SomeMethod();
      result.Wait();
  
      //---Assert--
  }
  </code></pre>
  
  <p><strong>Question</strong>: why is the stream in my method freezes during test execution???</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>This test begins to work if I replace <br/></p>
  
  <pre><code>var result = await unitOfWork.SomeRepository.GetAsync(); 
  </code></pre>
  
  <p>to</p>
  
  <pre><code>var models = unitOfWork.SomeRepository.GetAsync();
  models.Start();
  models.Wait();
  var result = models.Result;
  </code></pre>
  
  <p>But I don't quite understand why it works like that. Can someone explain?</p>