title: MVC - Moq Unit Test FileContentResult (ActionResult) - NullRefernceException
body: <p>So I'm posting to an MVC controller, which makes a call to a repository to get a Telerik report, then exports a PDF.  I'm having trouble unit testing this and keep getting an error - </p>
  
  <blockquote>
    <p>System.NullReferenceException: Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public class ReportController : Controller
  {
      private IPDFRepository _pdfRepository;
  
      //Dependency Injection using Unity.MVC5 NuGet Package
      public ReportController(IPDFRepository pdfRepository)
      {
          _pdfRepository = pdfRepository;
      }
  
      [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult PDFExport(PDFViewModel model)
      {
          byte[] report = _pdfRepository.BuildExport(model);
          return File(report, "application/pdf", model.SelectedReport + ".pdf");
      }
  }
  </code></pre>
  
  <p><strong>Unit Test</strong></p>
  
  <pre><code>[TestMethod]
  public void Report_PDFExport_Returns_ActionResult()
  {
      //Arrange
      var mockRepository = new Mock&lt;IPDFRepository&gt;();
      mockRepository.Setup(x =&gt; x.BuildExport(It.IsAny&lt;PDFViewModel&gt;()));
  
      ReportController controller = new ReportController(mockRepository.Object);
  
      //Act
      ActionResult result = controller.PDFExport(It.IsAny&lt;PDFViewModel&gt;());
  
      //Assert
      Assert.IsInstanceOfType(result, typeof(ActionResult));
  }
  </code></pre>
  
  <p>Now, I realize this has something to do with this return portion of my controller.</p>
  
  <pre><code>return File(report, "application/pdf", model.SelectedReport + ".pdf");
  </code></pre>
  
  <p>I can change that around to return string, test again and get this to work.</p>
  
  <p>Also, if I comment out these last two lines of the unit test,</p>
  
  <pre><code>//Act
  //ActionResult result = controller.PDFExport(It.IsAny&lt;PDFViewModel&gt;());
  
  //Assert
  //Assert.IsInstanceOfType(result, typeof(ActionResult));
  </code></pre>
  
  <p>it will run without error.  I can't figure out how to get around the null reference.</p>