Should I use AutoMapper in my unit tests?

前端 未结 2 1638
滥情空心
滥情空心 2021-02-20 11:30

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

Those controllers have a dependency on IMapper - an interface I\'ve created to abstract AutoMapp

2条回答
  •  一个人的身影
    2021-02-20 12:00

    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.

提交回复
热议问题