1

I have a controller method in my webapi project which calls a service method and converts the response of the service method into a Dto object for sending the response. The controller looks something like this:

    [HttpPost]
    [Route(WebApiConfig.RootApiUri + "/v1/examplepost")]
    public async Task<List<Html1Dto>> examplepost([FromBody] SomeInfoDto someInfoDto)
    {

        var someInfo = _mapper.Map<SomeInfoDto, SomeInfo>(someInfoDto);

        return this._mapper.Map<List<Html1>, List<Html1Dto>>(await this._someService.SomeCall(someInfo));
    }

and the mock test like this :

   //Arrange
   var mockMapper = new Mock<IMapper>();
   var mockSomeService = new Mock<ISomeService<Html1>>();

   mockSomeService.Setup(s => s.SomeCall(It.IsAny<SomeInfo>())).ReturnsAsync(
            new List<Html1>() {new Html1() {....}});
   SomeInfoDto mockSomeInfoDto = new SomeInfoDto()
        {
           ..
        };
        SomeInfo mockSomeInfo = new SomeInfo();
   mockMapper.Setup(m => m.Map<SomeInfoDto, SomeInfo>(mockSomeInfoDto))
                  .Returns(mockSomeInfo);
   mockMapper.Setup(m => m.Map<List<Html1>, List<Html1Dto>>(It.IsAny<List<Html1>>())).Returns(It.IsAny<List<Html1Dto>>);
   var someController = GetController(mockMapper.Object, mockSomeService.Object);
    //Act
    var result = await someController.examplePost(mockSomeInfoDto);

I am using automapper to map the objects with Dtos. When I debug this test, result comes as null. The mapping of incoming dto works fine. I suspect there is some issue with the service method setup. Anyhelp is appreciated.

1
  • One quick thing - don't mock AutoMapper. Really, it's pointless. It's like mocking JSON.Net or StringBuilder. Just use the real thing. Commented May 31, 2016 at 19:02

1 Answer 1

2

Your mapper mock is the other way round

mockMapper.Setup(m => m.Map<List<Html1>, List<Html1Dto>>(It.IsAny<List<Html1>>())).Returns(It.IsAny<List<Html1Dto>>);

to the signature in the method

this._mapper.Map<List<Html1Dto>, List<Html1>>(await this._someService.SomeCall(someInfo));

Additionally, assuming that is correct in your actual code, then the other bit that could be causing you issue is that the return It.IsAny<List<Html1Dto>> which will be null as default(List<HtmlDto>) is null, return a concrete class there instead as below.


This call:

this._mapper.Map<List<Html1Dto>, List<Html1>>(await this._someService.SomeCall(someInfo));

Doesn't have a setup in the Unit Test, so will return null. You need to arrange that to, probably something like:

mockMapper.Setup(m => m.Map<List<Html1Dto>,  List<Html1>>(It.IsAny<List<Html1>>()))
.ReturnsAsync(new List<Html1Dto> { ... });
Sign up to request clarification or add additional context in comments.

6 Comments

I already have such code in my test, forgot to add in the question, It's not working with this also. I have edited the question
Do you have a complete example you could share?
I couldn't post the exact original code, hence I have faked it. Except the objects mentioned, the code is pretty much the same. If you need any extra information please comment it and I will add it in the question.
What if you move the await this._someService.SomeCall(someInfo) to a variable on the line above and pass that variable in to the .Map method?
@stu : I was still getting result as null from the fix you mentioned. Thanks for your response.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.