Should I use AutoMapper in my unit tests?

一世执手 提交于 2019-12-04 03:41:51

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.

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.

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- found it, it's obj.SetupAllProperties();

I'm in favour of #2 like jeriley

Adding to the Moq, if you need to return an object based on values passed to it you can write your setup like so:

mockObject.Setup(x => x.MapObject(It.IsAny())
          .Returns((ProductDto productDto) => 
           {
               var product = new Product()
               {
                   Id = productDto.Id,
                   Name = productDto.Name
               };

               return product
           });

Little bit messy but handy.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!