Mocking Mapper.Map() in Unit Testing

I have following line of code in my controller and need to Setup this for Unit Test.

var result = data.ToList().Select(x=> this.mapper.Map<A_Class, B_Class>   (x)).ToList();

I am doign something like following

  this.mapperMock.Setup(x => x.Map<A_Class, B_Class>(AAA)).Returns(expectedResult);

Can anyone suggest what should be AAA and what should be expectedResult? In my controller my linq works foreach object of A_Class in Data. How can this be setup in UnitTest

Upvotes: 1

Views: 6391

Answers (1)

StuartLC
StuartLC

Reputation: 107327

If you want to return your fake expectedResult no matter what value of A_Class is passed:

mapperMock.Setup(x => x.Map<A_Class, B_Class>(It.IsAny<A_Class>))
          .Returns(expectedResult);

If you want to be more specific, e.g. just return expectedResult for mapped A_Class with a property value of 'foo':

mapperMock.Setup(
         x => x.Map<A_Class, B_Class>(It.Is<A_Class>(_ => a.Property == "foo")))
    .Returns(expectedResult);

Note that if no setup matches, Moq will return a default value, which will be null for a reference type.

Upvotes: 1

Related Questions