使用viewModel测试编辑操作

时间:2012-11-21 11:41:51

标签: asp.net-mvc unit-testing nunit moq automapper

我有以下控制器操作

public ActionResult EditFocus(int id)
    {
        var Focus = focusService.GetFocus(id);
        Mapper.CreateMap<Focus, FocusFormModel>()
        FocusFormModel editFocus = Mapper.Map<Focus, FocusFormModel>(Focus);
        if (Focus == null)
        {
            return HttpNotFound();
        }
        return View("EditFocus", editFocus);

我正在使用Nunit进行测试。测试是

 [Test]
public void Edit_Get_ReturnsView()
{


    FocusFormModel group = new FocusFormModel() { FocusId = 1, FocusName = "Test" };
    Mapper.CreateMap<Focus, FocusFormModel>().ForAllMembers(opt => opt.Ignore());
    Focus focusViewModel = Mapper.Map<Focus, FocusFormModel>(group);
    Mapper.AssertConfigurationIsValid();
    focusRepository.Setup(x => x.GetById(1)).Returns(focusViewModel);

    FocusController controller = new GroupController(focusService);
    ViewResult actual = controller.EditFocus(1) as ViewResult;
    Assert.IsNotNull(actual, "View Result is null");

}

但是测试失败了 在NUnit GUI中,我收到以下错误:

Missing Mapping type Configuration& Unsupported Mapping

1 个答案:

答案 0 :(得分:1)

您正在创建两次映射。只应对网络测试执行一次。

网络:仅在应用程序启动时创建一次地图,可能使用profiles

测试:在特定测试中的TestFixtureSetUp或基础测试类中创建地图(如果有)。