How to mock Automapper (IMapper) in controller

不羁的心 提交于 2021-01-22 05:43:13

问题


I am trying to write a unit test for my existing MVC Web Aplication. In that I am facing some problem in automapper (IMapper) Whenever am using map function it returns null value.

My Controller Code:

public class UserAdministrationController : BaseController
{
    private readonly iUserService _userService;
    private readonly IMapper _mapper;

    public NewsController(iUserService userService, IMapper mapper)
    {
        _userService = userService;
        _mapper = mapper;
    }

    public ActionResult Create(int CompanyID == 0)
    {            
        UserDetail data = _userService(CompanyID);
        var Modeldata = _mapper.Map<UserDetailViewModel, UserDetail>(data);
        return View(Modeldata);
    }
} 

Mock Mapping Code:

public class MappingDataTest : CommonTestData
{
    public Mock<IMapper> MappingData()
    {
        var mappingService = new Mock<IMapper>();
        UserDetailViewModel interview = getUserDetailViewModel(); // get value of UserDetailViewModel
        UserDetail im = getUserDetail(); // get value of UserDetails

        mappingService.Setup(m => m.Map<UserDetail, UserDetailViewModel>(im)).Returns(interview);
        mappingService.Setup(m => m.Map<UserDetailViewModel, UserDetail>(interview)).Returns(im);

        return mappingService;
    }
}

Mocking Code:

[TestClass]
public class UserAdminControllerTest
{
    private MappingDataTest _common;

    [TestInitialize]
    public void TestCommonData()
    {
        _common = new MappingDataTest();
    }

    [TestMethod]
    public void UserCreate()
    {
        //Arrange                                               
        UserAdministrationController controller = new UserAdministrationController(_common.mockUserService().Object, _common.MappingData().Object);
        controller.ControllerContext = _common.GetUserIdentity(controller);

        // Act
        ViewResult newResult = controller.Create() as ViewResult;

        // Assert
        Assert.IsNotNull(newResult);
    }
}

Mapper is not working its always showing the null value in controller. kindly help me. Thanks in Advance.


回答1:


You should try the following:

public class MappingDataTest : CommonTestData
{
    public Mock<IMapper> MappingData()
    {
        var mappingService = new Mock<IMapper>();

        UserDetail im = getUserDetail(); // get value of UserDetails

        mappingService.Setup(m => m.Map<UserDetail, UserDetailViewModel>(It.IsAny<UserDetail>())).Returns(interview); // mapping data
        mappingService.Setup(m => m.Map<UserDetailViewModel, UserDetail>(It.IsAny<UserDetailtViewModel>())).Returns(im); // mapping data

        return mappingService;
    }
}

The thing is, your mock was expecting the exact instance of UserDetailViewModel interview = getUserDetailViewModel(); to setup this mapping, and this is why it was returning null. Null it will be expecting any reference to UserDetailViewModel and for any reference to UserDetailtViewModel it will return the expected mapped instance.




回答2:


I would recommend not mocking AutoMapper. There's not much value in controller unit tests for one, and this is similar to mocking a JSON serializer. Just use the real thing.



来源:https://stackoverflow.com/questions/43817242/how-to-mock-automapper-imapper-in-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!