Problems in creating unit test for ASP .NET MVC
<p>I am creating some unit tests for my ASP .NET MVC Controller class and I ran into some very strange errors:</p>

<p>My controller code is below:</p>

<pre><code>[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(JournalViewModel journal)
{
    var selectedJournal = Mapper.Map&lt;JournalViewModel, Journal&gt;(journal);

    var opStatus = _journalRepository.DeleteJournal(selectedJournal);
    if (!opStatus.Status)
        throw new System.Web.Http.HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));

    return RedirectToAction("Index");
}
</code></pre>

<p>My test code is below:</p>

<pre><code>[TestMethod]
public void Delete_Journal()
{
    // Arrange

    // Simulate PDF file
    HttpPostedFileBase mockFile = Mock.Create&lt;HttpPostedFileBase&gt;();
    Mock.Arrange(() =&gt; mockFile.FileName).Returns("Test.pdf");
    Mock.Arrange(() =&gt; mockFile.ContentLength).Returns(255);

    // Create view model to send.
    JournalViewModel journalViewModel = new JournalViewModel();
    journalViewModel.Id = 1;
    journalViewModel.Title = "Test";
    journalViewModel.Description = "TestDesc";
    journalViewModel.FileName = "TestFilename.pdf";
    journalViewModel.UserId = 1;
    journalViewModel.File = mockFile; // Add simulated file

    Mock.Arrange(() =&gt; journalRepository.DeleteJournal(null)).Returns(new OperationStatus
    {
        Status = true
    });

    // Act
    PublisherController controller = new PublisherController(journalRepository, membershipRepository);
    RedirectToRouteResult result = controller.Delete(journalViewModel) as RedirectToRouteResult;

    // Assert
    Assert.AreEqual(result.RouteValues["Action"], "Index");
}
</code></pre>

<p><strong>Problem 1 - Mapping Exception:</strong></p>

<p>Every time I run my test I receive the following exception:</p>

<blockquote>
  <p>Test Name:    Delete_Journal Test<br>
  FullName: Journals.Web.Tests.Controllers.PublisherControllerTest.Delete_Journal<br>
  Test Source:  \Source\Journals.Web.Tests\Controllers\PublisherControllerTest.cs
  : line 132<br>
  Test Outcome: Failed Test Duration:   0:00:00,3822468</p>
  
  <p>Result StackTrace:     at
  Journals.Web.Controllers.PublisherController.Delete(JournalViewModel
  journal) in
  \Source\Journals.Web\Controllers\PublisherController.cs:line 81    at
  Journals.Web.Tests.Controllers.PublisherControllerTest.Delete_Journal()
  in
  \Source\Journals.Web.Tests\Controllers\PublisherControllerTest.cs:line
  156 Result Message:    Test method
  Journals.Web.Tests.Controllers.PublisherControllerTest.Delete_Journal
  threw exception:  AutoMapper.AutoMapperMappingException: Missing type
  map configuration or unsupported mapping.</p>
  
  <p>Mapping types: JournalViewModel -> Journal
  Journals.Model.JournalViewModel -> Journals.Model.Journal</p>
  
  <p>Destination path: Journal</p>
  
  <p>Source value: Journals.Model.JournalViewModel</p>
</blockquote>

<p>It seems that there is a mapping problem between the classes <code>JournalViewModel</code> and <code>Journal</code>, however I don't know where that is. I added this code to the <code>Application_Start</code> in <code>Global.asax.cs</code>:</p>

<pre><code>Mapper.CreateMap&lt;Journal, JournalViewModel&gt;();
Mapper.CreateMap&lt;JournalViewModel, Journal&gt;();
</code></pre>

<p>And mapping from <code>Journal</code> to <code>JournalViewModel</code> is working.</p>

<p>In the end I tried adding <code>Mapper.CreateMap&lt;JournalViewModel, Journal&gt;();</code> as the first line of the <code>Delete</code> method and then everything works, however I am not sure why.</p>

<p><strong>Problem 2 - HTML Exception</strong></p>

<p>Once the mapping is running with the workaround above, I have a problem in which the property <code>Status</code> from <code>var opStatus = _journalRepository.DeleteJournal(selectedJournal);</code> is always false, even though I used Mock to override it and make it always true. This causes the throwing of an HTML Exception that shouldn't happen.</p>

<p><strong>EDIT</strong></p>

<p>I changed in my Application_Start to:</p>

<pre><code>Mapper.Initialize(cfg =&gt;
{
    cfg.CreateMap&lt;Journal, JournalViewModel&gt;();
    cfg.CreateMap&lt;JournalViewModel, Journal&gt;();
});
</code></pre>

<p>But I still have the same error.</p>

<p><strong>EDIT - Problem 2 Solved</strong></p>

<p>It turns out that I forgot to add the mapping to my unit test class, so I did the following:</p>

<pre><code>[TestInitialize]
public void TestSetup()
{
    // Create necessary mappings
    Mapper.CreateMap&lt;Journal, JournalViewModel&gt;();
    Mapper.CreateMap&lt;JournalViewModel, Journal&gt;();

    //...other code omitted for brevity
}
</code></pre>

<p>And it turns out that this was the source of the problem. I think that since the Global.asax.cs Application_Start() is never called in the unit tests, the Mapping is never created, so I had to do this myself in the unit tests initialization.</p>