1

Net core application. I have below controller method

public async Task<ActionResult> UpdateMapFile(int mapId, MapFileDetailsBusinessModel mapFileUploadActionsEntity)
{
    var result = await _geoClient.SendRequestMapFileUpload<string, UpdateMapFileRequestModel>(
        new UpdateMapFileRequestModel
        {
            MapFile = mapFileUploadActionsEntity,
            MapId = mapId

        }, "updatemapfile", HttpMethod.Put).ConfigureAwait(false);
    return Ok(result);
}

This will return ActionResult type. The signature for SendRequestMapFileUpload as follows

Task<ActionResult> SendRequestMapFileUpload<T,U>(U request, string methodName, HttpMethod reqtype);

This will return Task<ActionResult>. Below is my unit test case

Below is implementation of geo client

public async Task<ActionResult> SendRequestMapFileUpload<T,U>(U request, string methodName, HttpMethod reqtype)
            {
                return await SendRequest<T,U>(request, "/api/MapFileUpload/" + methodName, reqtype, _token);
            }

Below is test case

[Test]
public async Task UpdateMapFile_ExpectedBehavior()
{
    _mapFileUploadController.BuildMockControllerContext();
    _geoClient.Setup(x => x.SendRequestMapFileUpload<string, UpdateMapFileRequestModel>(updateMapFileRequestModel, It.IsAny<string>(), HttpMethod.Put))
        .Returns(//here how to return ActionResult);
    var result = await _mapFileUploadController.UpdateMapFile(mapId, mapFileDetailsBusinessModel);
    var statusCode = result as NoContentResult;
    Assert.AreEqual(204, statusCode.StatusCode);
}

In the above method When I am mocking I have to return ActionResult. Here I am struggling what exactly and how to return ActionResult. can someone help me regarding this. Any help would be appreciated. Thank you

Mr Perfect
  • 585
  • 8
  • 30
  • Will need to get some more context information about `geoClient`. Are you incontrol of that type? If your are, why is it returning `ActionResult`. That type should really only be return by controllers and their related helpers. – Nkosi Mar 31 '21 at 11:55
  • Hi Nkosi I have added implemenation for geoclient. Above code is in my application gateway controller which calls another application controller. Here return type will be not fixed so returning action result – Mr Perfect Mar 31 '21 at 15:29
  • In that case then, mock the `ActionResult` and have the mock client return that – Nkosi Mar 31 '21 at 16:39
  • 1
    I must say it is confusing having the other calls returning an ActionResult as well since it is being wrapped by another actionresult in the controller. – Nkosi Mar 31 '21 at 16:40
  • Hi Nkosi. I can give more details in https://stackoverflow.com/questions/66782707/how-to-return-generic-response-from-controller/66783941?noredirect=1#comment118076182_66783941 – Mr Perfect Apr 01 '21 at 12:01

0 Answers0