如何为控制器使用automapper编写单元测试?

Khi*_*yen 0 asp.net-mvc nunit unit-testing automapper

我正在尝试为控制器编写单元测试以测试方法返回所有用户.但我很困惑如何用automapper编写单元测试

控制器:

private readonly IUserService _userService;

public UserController(IUserService userService)
{
  this._userService = userService;
}

public ActionResult List()
{
  var users = _userService.GetAllUsers().ToList();
  var viewModel = Mapper.Map<List<UserViewModel>>(users);
  return View(viewModel);
}
Run Code Online (Sandbox Code Playgroud)

控制器测试:

    private Mock<IUserService> _userServiceMock;
    UserController objUserController;
    List<UserViewModel> listUser;

    [SetUp]
    public void Initialize()
    {
        _userServiceMock = new Mock<IUserService>();
        objUserController = new UserController(_userServiceMock.Object);
        listUser = new List<UserViewModel>()
        {
            new UserViewModel() {Id = 1, Active = true, Password = "123456", UserName = "hercules"},
            new UserViewModel() {Id = 2, Active = false, Password = "1234567", UserName = "alibaba"},
            new UserViewModel() {Id = 3, Active = true, Password = "12345678", UserName = "robinhood"},
        };
    }

    [Test]
    public void Index_Returns_AllUser()
    {
      // How do I here ???
    }
Run Code Online (Sandbox Code Playgroud)

D.R*_*ado 6

像在MVC项目上一样配置automapper:

[SetUp]
public void Initialize()
{
    ....
    AutoMapperConfiguration.Configure();
}
Run Code Online (Sandbox Code Playgroud)

AutoMapperConfiguration是一个公共静态类,如:

public class AutoMapperConfiguration
{

    /// <summary>
    /// Maps between VIEWMODEL and MODEL
    /// </summary>
    public static void Configure()
    {
        //maps here
         Mapper.CreateMap..
    }
}
Run Code Online (Sandbox Code Playgroud)