查看模型到领域模型,在哪里测试映射?
View model to domain model, where to test the mapping?
我目前正在 MVC 项目中试验映射框架(类似自动映射器)。
在我的控制器中,我使用框架的扩展方法 "Map" 将视图模型映射到域模型(似乎是合法的地方)。
当然,如果映射中断(例如,如果某些 属性 名称更改并导致不匹配),我的代码将无法工作。
但是在哪里可以按预期测试映射工作?
控制器不是 "unit" 负责的。视图模型和领域模型都不是。
虽然我可以创建自己的包装器(Ioc 可注入实例)并使用视图模型到域模型的映射对其进行单元测试,但感觉有点尴尬(代码读者怎么知道那些测试需要在特定的类?).
我有点迷路了。
编辑:(反思 John Mc 的回答)
using System;
using NSubstitute;
using Models = TestEncodeLines.Models;
using Controllers = TestEncodeLines.Controllers;
using ViewModels = TestEncodeLines.ViewModels;
using Infrastructure = TestEncodeLines.Infrastructure;
using Xunit;
namespace Tests.TestControllers
{
public class TestActivityController
{
private Controllers.ActivityController _controller;
public TestActivityController()
{
_controller = new Controllers.ActivityController();
}
[Fact]
public void Save_Project()
{
// Arrange
var viewModel = new ViewModels.ActivitiesViewModel();
var model = Substitute.For<Models.IActivitiesModel>();
var mapper =
Substitute.For<Infrastructure.IMapper<ViewModels.ActivitiesViewModel, Models.IActivitiesModel>>();
mapper.Map(viewModel).Returns(model);
// Act
_controller.SaveActivities(viewModel);
// Assert
model.Received().Save();
}
[Fact]
public void Save_Project_TestMapping /* Here ??? */ ()
{
// Arrange
var viewModel = new ViewModels.ActivitiesViewModel
{
Activities = new[]
{
new ViewModels.ActivitiesViewModel.Project
{
From = new DateTime(2016, 02, 23, 8, 0, 0, DateTimeKind.Utc),
To = new DateTime(2016, 02, 23, 10, 0, 0, DateTimeKind.Utc),
Name = "Test"
}
},
Date = new DateTime(2016, 02, 23, 0, 0, 0, DateTimeKind.Utc)
};
var mapper = new Infrastructure.Mapper<ViewModels.ActivitiesViewModel, Models.IActivitiesModel>();
// Act
_controller.SaveActivities(viewModel);
// Assert
// Somehow (https://github.com/jamesfoster/DeepEqual ??) check the mapping
}
}
}
我将实现映射框架抽象为通过构造函数注入的IMappingService。然后,您可以确保在控制器单元测试中对映射框架进行了所需的调用。
至于测试映射,Automapper 有一个配置选项,可确保您配置的映射配置文件不会失效:
AutoMapper.AssertConfigurationIsValid()
但这并不是说映射是正确的。
你能不能只创建只关注映射部分的映射特定单元测试?为什么不实例化您的源对象并尝试将其映射到单元测试中的目标,并在那里断言它们的正确性?
我目前正在 MVC 项目中试验映射框架(类似自动映射器)。
在我的控制器中,我使用框架的扩展方法 "Map" 将视图模型映射到域模型(似乎是合法的地方)。
当然,如果映射中断(例如,如果某些 属性 名称更改并导致不匹配),我的代码将无法工作。
但是在哪里可以按预期测试映射工作?
控制器不是 "unit" 负责的。视图模型和领域模型都不是。
虽然我可以创建自己的包装器(Ioc 可注入实例)并使用视图模型到域模型的映射对其进行单元测试,但感觉有点尴尬(代码读者怎么知道那些测试需要在特定的类?).
我有点迷路了。
编辑:(反思 John Mc 的回答)
using System;
using NSubstitute;
using Models = TestEncodeLines.Models;
using Controllers = TestEncodeLines.Controllers;
using ViewModels = TestEncodeLines.ViewModels;
using Infrastructure = TestEncodeLines.Infrastructure;
using Xunit;
namespace Tests.TestControllers
{
public class TestActivityController
{
private Controllers.ActivityController _controller;
public TestActivityController()
{
_controller = new Controllers.ActivityController();
}
[Fact]
public void Save_Project()
{
// Arrange
var viewModel = new ViewModels.ActivitiesViewModel();
var model = Substitute.For<Models.IActivitiesModel>();
var mapper =
Substitute.For<Infrastructure.IMapper<ViewModels.ActivitiesViewModel, Models.IActivitiesModel>>();
mapper.Map(viewModel).Returns(model);
// Act
_controller.SaveActivities(viewModel);
// Assert
model.Received().Save();
}
[Fact]
public void Save_Project_TestMapping /* Here ??? */ ()
{
// Arrange
var viewModel = new ViewModels.ActivitiesViewModel
{
Activities = new[]
{
new ViewModels.ActivitiesViewModel.Project
{
From = new DateTime(2016, 02, 23, 8, 0, 0, DateTimeKind.Utc),
To = new DateTime(2016, 02, 23, 10, 0, 0, DateTimeKind.Utc),
Name = "Test"
}
},
Date = new DateTime(2016, 02, 23, 0, 0, 0, DateTimeKind.Utc)
};
var mapper = new Infrastructure.Mapper<ViewModels.ActivitiesViewModel, Models.IActivitiesModel>();
// Act
_controller.SaveActivities(viewModel);
// Assert
// Somehow (https://github.com/jamesfoster/DeepEqual ??) check the mapping
}
}
}
我将实现映射框架抽象为通过构造函数注入的IMappingService。然后,您可以确保在控制器单元测试中对映射框架进行了所需的调用。
至于测试映射,Automapper 有一个配置选项,可确保您配置的映射配置文件不会失效:
AutoMapper.AssertConfigurationIsValid()
但这并不是说映射是正确的。
你能不能只创建只关注映射部分的映射特定单元测试?为什么不实例化您的源对象并尝试将其映射到单元测试中的目标,并在那里断言它们的正确性?