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 - System.NullReferenceException: Object reference not set to an instance of an object
.
Controller
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");
}
}
Unit Test
[TestMethod]
public void Report_PDFExport_Returns_ActionResult()
{
//Arrange
var mockRepository = new Mock<IPDFRepository>();
mockRepository.Setup(x => x.BuildExport(It.IsAny<PDFViewModel>()));
ReportController controller = new ReportController(mockRepository.Object);
//Act
ActionResult result = controller.PDFExport(It.IsAny<PDFViewModel>());
//Assert
Assert.IsInstanceOfType(result, typeof(ActionResult));
}
Now, I realize this has something to do with this return portion of my controller.
return File(report, "application/pdf", model.SelectedReport + ".pdf");
I can change that around to return string, test again and get this to work.
Also, if I comment out these last two lines of the unit test,
//Act
//ActionResult result = controller.PDFExport(It.IsAny<PDFViewModel>());
//Assert
//Assert.IsInstanceOfType(result, typeof(ActionResult));
it will run without erroring out. I can't figure out how to get around the null reference.
Aucun commentaire:
Enregistrer un commentaire