【问题标题】:How to mock Automapper (IMapper) in controller如何在控制器中模拟 Automapper (IMapper)
【发布时间】:2017-05-10 01:04:27
【问题描述】:

我正在尝试为我现有的 MVC Web 应用程序编写单元测试。因为我在 automapper (IMapper) 中遇到了一些问题,每当我使用 map 函数时,它都会返回 null 值。

我的控制器代码:

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

模拟映射代码:

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

模拟代码:

[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 无法正常工作,它总是在控制器中显示null 值。请帮助我。提前致谢。

【问题讨论】:

  • 可能想问,“我为什么需要这样做?” Automapper 可以断言映射配置是正确的。大概您的用户服务已经过测试。因此无需测试您的控制器。
  • 是的,没关系。这是我的客户要求,他们也要求对控制器进行单元测试。然后在 httppost 它可能有一些附加功能,所以我需要这样做,请帮我解决这个问题。

标签: c# asp.net-mvc unit-testing mocking automapper


【解决方案1】:

我建议不要模拟 AutoMapper。一个控制器单元测试没有太大价值,这类似于模拟 JSON 序列化程序。用真品就好了。

【讨论】:

  • 我觉得 OP 应该听你的。你可能对这个主题有一些了解。 :-)
  • 我同意你的观点,尽管在控制器中使用具体实现让我很困扰。
【解决方案2】:

您应该尝试以下方法:

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

问题是,您的模拟期望 UserDetailViewModel interview = getUserDetailViewModel(); 的确切实例设置此映射,这就是它返回 null 的原因。 Null 它将期望对 UserDetailViewModel 的任何引用,并且对于对 UserDetailtViewModel 的任何引用,它将返回预期的映射实例。

【讨论】:

  • 它显示错误消息“无法从'方法组'转换为'用户详细信息'”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多