3

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));
}
4

1 回答 1

2

在您的测试中,您的 Interview 和 InterviewModel 类在 mappingService.Setup() 调用中交叉(顺便说一句,我认为您可以使用更好的命名约定,或者不使用 var,以保持对象清晰 - “ im"、"interview" 和 "interview1" 不容易理解哪个是模型,哪个是视图对象)。

尝试这个:

[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));
}
于 2012-11-21T15:20:48.300 回答