简体   繁体   English

在ASP.NET MVC应用程序中测试AutoMapper配置的方法是什么?

[英]What approach to take for testing AutoMapper configuration in an ASP.NET MVC application?

We are using AutoMapper extensively in our ASP.NET MVC web applications with the AutoMapViewResult approach set out in this question . 我们在ASP.NET MVC Web应用程序中广泛使用AutoMapper ,并在此问题中提出了AutoMapViewResult方法。 So we have actions that look like this: 所以我们的行为看起来像这样:

public ActionResult Edit(User item)
{
    return AutoMapView<UserEditModel>(View(item));
}

This creates hidden failure points in the application if the requested mapping has not been configured - in that this is not a compile time fail. 如果尚未配置请求的映射,则会在应用程序中创建隐藏的故障点 - 因为这不是编译时失败。

I'm looking at putting something in place to test these mappings. 我正在考虑放置一些东西来测试这些映射。 As this needs to test the actual AutoMapper configuration I presume this should be done as part of integration testing? 由于这需要测试实际的AutoMapper配置,我认为这应该作为集成测试的一部分来完成? Should these tests be structured per controller or per entity? 这些测试应该按照控制器还是按实体构建? What about the possibility of automatically parsing all calls to AutoMapView? 如何自动解析对AutoMapView的所有调用?

Note that we are already testing that the AutoMapper configuration is valid using AssertConfigurationIsValid, it is missing mappings that I want to deal with. 请注意,我们已经使用AssertConfigurationIsValid测试AutoMapper配置是否有效,它缺少我想要处理的映射。

If your controller action looked like this: 如果您的控制器操作如下所示:

public AutoMapView<UserEditModel> Edit(User item)
{
    return AutoMapView<UserEditModel>(View(item));
}

Then you can pretty easily, using reflection, look for all controller actions in your project. 然后,您可以非常轻松地使用反射查找项目中的所有控制器操作。 You then examine the action parameter types and the generic type parameter of your AutoMapView action result. 然后,检查AutoMapView操作结果的操作参数类型和泛型类型参数。 Finally, you ask AutoMapper if it has a type map for those input/output models. 最后,您询问AutoMapper是否具有这些输入/输出模型的类型映射。 AutoMapper doesn't have a "CanMap" method, but you can use the IConfigurationProvider methods of FindTypeMapFor: AutoMapper没有“CanMap”方法,但您可以使用FindTypeMapFor的IConfigurationProvider方法:

((IConfigurationProvider) Mapper.Configuration).FindTypeMapFor(null, typeof(User), typeof(UserEditModel);

Just make sure that's not null. 只要确保它不是空的。

I do something like this. 我这样做。

using System.Linq;
using System.Reflection;
using AutoMapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;

public class SampleDto
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Sample
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string LoginId { get; set; }
}

public class AutomapperConfig
{
    public static void Configure()
    {
        Mapper.Initialize(cfg => cfg.AddProfile<ViewModelProfile>());
    }
}

public class ViewModelProfile : Profile
{
    protected override void Configure()
    {
        CreateMap<SampleDto, Sample>();
    }
}

[TestClass]
public class AutoMapperTestsSample
{
    public AutoMapperTestsSample()
    {
        AutomapperConfig.Configure();
    }


    [TestMethod]
    public void TestSampleDtoFirstName()
    {
        #region Arrange
        var source = new SampleDto();
        source.FirstName = "Jim";
        //source.LastName = "Bob";
        var dest = new Sample();
        dest.FirstName = "FirstName";
        dest.LastName = "LastName";
        dest.LoginId = "LoginId";
        #endregion Arrange

        #region Act
        AutoMapper.Mapper.Map(source, dest);
        #endregion Act

        #region Assert
        Assert.AreEqual("Jim", dest.FirstName);
        Assert.AreEqual(null, dest.LastName);
        Assert.AreEqual("LoginId", dest.LoginId);
        #endregion Assert       
    }

    [TestMethod]
    public void TestSampleDtoLastName()
    {
        #region Arrange
        var source = new SampleDto();
        //source.FirstName = "Jim";
        source.LastName = "Bob";
        var dest = new Sample();
        dest.FirstName = "FirstName";
        dest.LastName = "LastName";
        dest.LoginId = "LoginId";
        #endregion Arrange

        #region Act
        AutoMapper.Mapper.Map(source, dest);
        #endregion Act

        #region Assert
        Assert.AreEqual(null, dest.FirstName);
        Assert.AreEqual("Bob", dest.LastName);
        Assert.AreEqual("LoginId", dest.LoginId);
        #endregion Assert
    }

    /// <summary>
    /// This lets me know if something changed in the Dto object so I know to adjust my tests
    /// </summary>
    [TestMethod]
    public void TestSampleDtoReflection()
    {
        #region Arrange
        var xxx = typeof(SampleDto);
        #endregion Arrange

        #region Act
        #endregion Act

        #region Assert
        Assert.AreEqual(2, xxx.GetRuntimeFields().Count());
        Assert.AreEqual("System.String", xxx.GetRuntimeFields().Single(a => a.Name.Contains("FirstName")).FieldType.ToString());
        Assert.AreEqual("System.String", xxx.GetRuntimeFields().Single(a => a.Name.Contains("LastName")).FieldType.ToString());            
        #endregion Assert       
    }
}

You can use the AssertConfigurationIsValid method. 您可以使用AssertConfigurationIsValid方法。 Details are on the automapper codeplex site ( http://automapper.codeplex.com/wikipage?title=Configuration%20Validation ) 详细信息在automapper codeplex站点上( http://automapper.codeplex.com/wikipage?title=Configuration%20Validation

Strictly speaking you should be writing a test to validate the mapping before you write a controller action that depends on the mapping configuration being present. 严格来说,在编写依赖于存在的映射配置的控制器操作之前,您应该编写一个测试来验证映射。

Either way, you can use the Test Helpers in the MvcContrib project to check the action method returns the expected ViewResult and Model. 无论哪种方式,您都可以使用MvcContrib项目中的Test Helpers来检查action方法返回预期的ViewResult和Model。

Here's an example: 这是一个例子:

            pageController.Page("test-page")
            .AssertViewRendered()
                .WithViewData<PortfolioViewData>()
                    .Page
                        .ShouldNotBeNull()
                        .Title.ShouldEqual("Test Page");
    [Test]
    public void MapperConfiguration()
    {
        var mapper = Web.Dto.Mapper.Instance;
        AutoMapper.Mapper.AssertConfigurationIsValid();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单元测试依赖于AutoMapper和类型转换器的ASP.NET MVC应用程序服务 - Unit testing ASP.NET MVC application services that rely on AutoMapper and type converters 如何在 ASP.NET MVC 控制器中使用 Automapper 配置 - How to use Automapper configuration in ASP.NET MVC controllers Asp.net mvc 3 应用程序的配置设置 - Configuration Setup for Asp.net mvc 3 application 单元测试 ASP.NET MVC 3 应用程序 - Unit testing an ASP.NET MVC 3 application 集成测试ASP.NET MVC应用程序 - Integration testing an ASP.NET MVC application 单元测试Asp.Net MVC路线:选择哪种方法? - Unit testing Asp.Net MVC routes: which approach to choose? asp.net mvc和jquery对话框:我应采用哪种方法将项目添加到dropdownlist / select列表而不进行完整的postaback? - asp.net mvc & jquery dialog: What approach do I take to add items to a dropdownlist/select list without full postaback? 创建asp.net MVC和WCF服务应用程序的建议方法 - Suggested approach for creating asp.net MVC and WCF service application asp.net mvc支持什么方法? - What approach does asp.net mvc supports? 本地化 Static 文本 asp.net mvc3. 有什么办法? - Localizing Static Text asp.net mvc3.What is the approach?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM