gpt4 book ai didi

c# - 在使用 Automapper 映射 ViewModel 之后,我应该如何测试以及应该测试什么?

转载 作者:可可西里 更新时间:2023-11-01 08:41:01 25 4
gpt4 key购买 nike

我正在尝试测试 Controller 的 Index 操作。该操作使用 AutoMapper将域 Customer 对象映射到 View 模型 TestCustomerForm。虽然这有效,但我关心的是测试我从 Index 操作收到的结果的最佳方法。

Controller 的索引操作如下所示:

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

CreateTestController 方法中,我使用 Rhino.Mocks 模拟客户存储库并将其设置为从 SetupCustomerForRepository 返回客户。通过这种方式,我知道当 Index 操作调用 _repository.GetCustomerByLogin(CurrentUserLoginName) 时,存储库将返回目标客户。因此,我认为断言相等计数足以满足 IndexShouldReturnCustomerWithMachines

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

  1. result.ViewData.Model 转换为 TestCustomerForm 似乎很冒昧。这真的是个问题吗?这让我很担心,因为在这种情况下,我并没有真正进行测试驱动开发,而且我似乎指望特定的实现来满足测试。
  2. 是否有更合适的测试来确保正确映射?
  3. 我应该测试 TestCustomerForm 中的每个映射属性吗?
  4. 我应该做更多的通用 Controller Action 测试吗?

最佳答案

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

这类似于测试 ViewResult。您不从 Controller 测试 View 是否已呈现,而是告诉 MVC 呈现某某 View 。我们的行动结果变成:

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

在基 Controller 类上使用辅助方法:

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

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

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

关于c# - 在使用 Automapper 映射 ViewModel 之后,我应该如何测试以及应该测试什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3082179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com