我应该在单元测试中使用AutoMapper? [英] Should I use AutoMapper in my unit tests?

查看:550
本文介绍了我应该在单元测试中使用AutoMapper?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写单元测试ASP.NET MVC控制器方法。

I'm writing unit tests for ASP.NET MVC controller methods.

这些控制器对 IMapper - 我创建抽象AutoMapper一个接口,通过了经使用温莎城堡的构造函数注入

Those controllers have a dependency on IMapper - an interface I've created to abstract AutoMapper, passed in via constructor injection using Castle Windsor.

操作方法使用 IMapper 从域对象视图模型对象,然后再返回映射,旨在让事情变得干燥,保持动作方法简洁。

Action methods use IMapper to map from domain objects to ViewModel objects and back again, aiming to keep things DRY and keep action methods succinct.

在我的单元测试,我应该

In my unit tests, should I


  1. 配置AutoMapper用正确的绑定(他们使用AutoMapper配置文件创建的,因此可测试和网站和单元测试项目之间可以重用)在传递一个正确的AutoMapper实施 IMapper 。在模拟对象

通(I'使用起订量)M为 IMapper 例如,根据测试(这将意味着重复测试设置代码的一些工作,以确保物体模拟映射器返回涉及到模拟映射是假装的对象映射)。

Pass in mock objects (I'm using Moq) for the IMapper instance, depending on the test (this would mean duplicating some work in the test setup code to make sure the objects returned from the mock mapper relate to the objects the mock mapper is pretending to map).

手工配置AutoMapper只有我想我需要为每个测试(很多工作的映射并意味着我没有测试,将真正在使用的映射)。

Hand-configure AutoMapper with just the mappings I think I'll need for each test (lot of work and means I'm not testing the mappings that will really be in use).

什么是使用基础设施的意见在单元测试代码?在什么时候它成为一个集成测试(即AutoMapper和我的控制器检测一体化)?

What's the opinion on using infrastructure code in unit tests? At what point does it become an integration test (i.e. testing integration of AutoMapper and my controllers)?

感觉就像2是纯粹的观点,但我想我需要了解更多关于起订量,以及如何得到它返回,涉及到传递到了它的嘲笑方法的实际值的值。

It feels like 2 is the purist view, though I think I need to learn more about Moq and how to get it to return values that relate to the actual values passed into the methods that it's mocking.

推荐答案

我倾向于#2同意。你知道automapper的作品,你知道你的作品注入我会更加注重细节,事情不只是SomeClass.Property = AnotherClass.Property(测试了为右:-)) - 这些特殊的情况下,应该没有测试基本复印功能。请不要在测试框架的东西。

I could tend to agree with #2. You know automapper works, you know your injection works ( got tests for that right? :-) ) I would focus more on specifics, things that aren't just SomeClass.Property = AnotherClass.Property -- THOSE special cases should be tested not basic copy functions. Don't test framework stuff.

至于更多的测试代码 - 我觉得这是完全OK。测试应该是给定的测试(也在情理之中),只是给定单元内设置。

As for more test code - I feel that's completely ok. Tests should be setup within the given tests (also within reason) for just that given unit.

关于起订量,语法很容易,不overthink它。 VAR OBJ =新莫克();然后设置你的属性,如obj.Setup(X => x.Property).returns(你好),除非你有一个更具体的问题?起订量也有安装在其上的所有属性一样,所以你可能甚至不需要automapper

Regarding Moq, syntax is easy, don't overthink it. var obj = new Mock(); then set your properties like obj.Setup(x => x.Property).returns("hello") unless you have a more specific issue? Moq also has setup all properties on it as well, so you may not even need automapper

-edit-发现了它,它的obj.SetupAllProperties();

-edit- found it, it's obj.SetupAllProperties();

这篇关于我应该在单元测试中使用AutoMapper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆