11

我正在尝试测试Index控制器的操作。该操作使用AutoMapper将域Customer对象映射到视图模型TestCustomerForm。虽然这可行,但我担心测试我从Index操作中收到的结果的最佳方法。

控制器的索引操作如下所示:

public ActionResult Index()
{
    TestCustomerForm cust = Mapper.Map<Customer,
        TestCustomerForm>(_repository.GetCustomerByLogin(CurrentUserLoginName));

    return View(cust);
}

TestMethod看起来像这样:

[TestMethod]
public void IndexShouldReturnCustomerWithMachines()
{
    // arrange
    var customer = SetupCustomerForRepository(); // gets a boiler plate customer
    var testController = CreateTestController();

    // act
    ViewResult result = testController.Index() as ViewResult;

    // assert
    Assert.AreEqual(customer.MachineList.Count(),
        (result.ViewData.Model as TestCustomerForm).MachineList.Count());
}

CreateTestControllerRhino.Mocks用来模拟客户存储库并将其设置为从SetupCustomerForRepository. 通过这种方式,我知道存储库将在Index操作调用时返回预期的客户_repository.GetCustomerByLogin(CurrentUserLoginName)。因此,我认为断言相等的计数足以满足IndexShouldReturnCustomerWithMachines.

所有这些都表明我担心我应该测试什么。

  1. 铸造result.ViewData.Model as TestCustomerForm. 这真的是一个问题吗?这让我很担心,因为在这种情况下,我并没有真正进行测试驱动开发,而且似乎我指望特定的实现来满足测试。
  2. 是否有更合适的测试来确保正确的映射?
  3. 我应该测试每个映射的属性TestCustomerForm吗?
  4. 我应该做更一般的控制器动作测试吗?
4

2 回答 2

15

这是我们将 AutoMapper 移动到自定义 ActionResult 或 ActionFilter 的原因之一。在某些时候,您只想测试您是否将 Foo 映射到 FooDto,但不一定要测试实际映射。通过将 AutoMapper 移动到层边界(例如在控制器和视图之间),您可以只测试您告诉 AutoMapper 做什么。

这类似于测试 ViewResult。您不会从控制器测试视图是否已呈现,而是您告诉 MVC 呈现某某视图。我们的动作结果变成:

public class AutoMapViewResult : ActionResult
{
    public Type SourceType { get; private set; }
    public Type DestinationType { get; private set; }
    public ViewResult View { get; private set; }

    public AutoMapViewResult(Type sourceType, Type destinationType, ViewResult view)
    {
        SourceType = sourceType;
        DestinationType = destinationType;
        View = view;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var model = Mapper.Map(View.ViewData.Model, SourceType, DestinationType);

        View.ViewData.Model = model;

        View.ExecuteResult(context);
    }
}

使用基本控制器类上的辅助方法:

protected AutoMapViewResult AutoMapView<TDestination>(ViewResult viewResult)
{
    return new AutoMapViewResult(viewResult.ViewData.Model.GetType(), typeof(TDestination), viewResult);
}

这使得控制器现在只指定映射到/从什么,而不是执行实际的映射:

public ActionResult Index(int minSessions = 0)
{
    var list = from conf in _repository.Query()
                where conf.SessionCount >= minSessions
                select conf;

    return AutoMapView<EventListModel[]>(View(list));
}

此时,我只需要测试,“确保您将此 Foo 对象映射到此目标 FooDto 类型”,而无需实际执行映射。

编辑:

这是一个测试片段的示例:

var actionResult = controller.Index();

actionResult.ShouldBeInstanceOf<AutoMapViewResult>();

var autoMapViewResult = (AutoMapViewResult) actionResult;

autoMapViewResult.DestinationType.ShouldEqual(typeof(EventListModel[]));
autoMapViewResult.View.ViewData.Model.ShouldEqual(queryResult);
autoMapViewResult.View.ViewName.ShouldEqual(string.Empty);
于 2010-06-21T12:23:26.577 回答
2

我可能会AutoMapper通过引入抽象来分离和控制器之间的耦合:

public interface IMapper<TSource, TDest>
{
    TDest Map(TSource source);
}

public CustomerToTestCustomerFormMapper: IMapper<Customer, TestCustomerForm>
{
    static CustomerToTestCustomerFormMapper()
    {
        // TODO: Configure the mapping rules here
    }

    public TestCustomerForm Map(Customer source)
    {
        return Mapper.Map<Customer, TestCustomerForm>(source);
    }
}

接下来,您将其传递给控制器​​:

public HomeController: Controller
{
    private readonly IMapper<Customer, TestCustomerForm> _customerMapper;
    public HomeController(IMapper<Customer, TestCustomerForm> customerMapper)
    {
        _customerMapper = customerMapper;
    }

    public ActionResult Index()
    {
        TestCustomerForm cust = _customerMapper.Map(
            _repository.GetCustomerByLogin(CurrentUserLoginName)
        );
        return View(cust);
    }
}

在你的单元测试中,你会使用你最喜欢的模拟框架来存根这个映射器。

于 2010-06-21T06:30:36.280 回答