Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moq Automapper service in testmethod returns null while mapping

I'm building a website in MVC 4 & using Automapper to map from domain objects to Viewmodel objects. I have injected Automapper as stated here http://rical.blogspot.in/2012/06/mocking-automapper-in-unit-testing.html

and it's working fine inside action methods while debugging, but during unit testing the action method when I inject automapper service I find that service.map is returning null. But while debugging the mapping is fine. I'm not being able to find the reason, trying for over 4 hrs. I have a domain class called Interview & its corrosponding viewmodel as InterviewModel. I have initialized mapping as CreateMap(); in automapper profile config, that has been called from global startup method. Below is the controller & action...

public class NewsAndViewsController : Controller
{
    private IInterviewRepository repository;
    private IMappingService mappingService;

    public NewsAndViewsController(IInterviewRepository productRepository, IMappingService autoMapperMappingService)
    {
        repository = productRepository;
        mappingService = autoMapperMappingService;
    }

    [HttpPost, ValidateAntiForgeryToken]
    [UserId]
    public ActionResult Edit(InterviewModel interView, string userId)
    {
        if (ModelState.IsValid)
        {
            var interView1 = mappingService.Map<InterviewModel, Interview>(interView);
            **// THE ABOVE LINE RETURNING NULL WHILE RUNNING THE BELOW TEST, BUT NOT DURING DEBUGGING**
            repository.SaveInterview(interView1);
            TempData["message"] = string.Format("{0} has been saved", interView.Interviewee);
            return RedirectToAction("Create");
        }
        return View(interView);
    }
}

[TestMethod]
public void AddInterview()
{
    // Arrange
    var interviewRepository = new Mock<IInterviewRepository>();
    var mappingService = new Mock<IMappingService>();
    var im = new InterviewModel { Interviewee="sanjay", Interviewer="sanjay", Content="abc" };
    mappingService.Setup(m => m.Map<Interview, InterviewModel>(It.IsAny<Interview>())).Returns(im);
    var controller = new NewsAndViewsController(interviewRepository.Object, mappingService.Object);

    // Act
    var result = controller.Edit(im, "2") as ViewResult;

    // Assert - check the method result type
    Assert.IsNotInstanceOfType(result, typeof(ViewResult));
}
like image 976
SanjayD Avatar asked Apr 21 '26 21:04

SanjayD


1 Answers

In your test you've got your Interview and InterviewModel classes crossed up in the mappingService.Setup() call (as an aside, I think you could use better naming conventions, or don't use var, to keep your objects clear - "im", "interview" and "interview1" don't make it easy to follow which is the model and which is the view object).

Try this:

[TestMethod]
public void AddInterview()
{
    // Arrange
    var interviewRepository = new Mock<IInterviewRepository>();
    var mappingService = new Mock<IMappingService>();
    var interview = new Interview();
    var im = new InterviewModel { Interviewee="sanjay", Interviewer="sanjay", Content="abc" };
    mappingService.Setup(m => m.Map<InterviewModel, Interview>(im).Returns(interview);
    var controller = new NewsAndViewsController(interviewRepository.Object, mappingService.Object);

    // Act
    var result = controller.Edit(im, "2") as ViewResult;

    // Assert - check the method result type
    Assert.IsNotInstanceOfType(result, typeof(ViewResult));
}
like image 90
Mike Parkhill Avatar answered Apr 23 '26 09:04

Mike Parkhill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!