1

I am trying to configure xUnit test for controller method. In my controller I am using MediatorR to make handler call. I have created the fixture of mediatorR Handler and set return expected object but not doing correctly and getting error on that

error

enter code here

Controller

public class MyController : ControllerBase
{

    private readonly IAppAmbientState appAmbientState;
    private readonly IMediator _mediator;

    public MyController (
        IAppAmbientState ambientState,
        IMediator mediator
       )
    {
        appAmbientState = ambientState;
        _mediator = mediator;
    }

   [HttpGet()]
    public async Task<IActionResult> GetHandHeldByIMEI(string imei)
    {
        try
        {
            var result = await _mediator.Send(new GetHandHeldByIMEI(imei));

            var returnResult = ResponseResultHelper.SuccessfulResult(result.HandHeld, result.ResultSummary);

            var serializeObject = JsonConvert.SerializeObject(returnResult, Formatting.None);

             return Content(serializeObject, "application/json");
        }
        catch (Exception e)
        {
            
        }
    }

Test

public class MyControllerTest
{
    private readonly MyController  sut;
    private readonly Mock<IMediator> mediatorMoq;
    private readonly Mock<IAppAmbientState> appAmbientStateMoq;
    

    public MyControllerTest()
    {
        mediatorMoq = new Mock<IMediator>();
        appAmbientStateMoq = new Mock<IAppAmbientState>();

        sut = new HandheldController(appAmbientStateMoq.Object, mediatorMoq.Object);
    }

    [Fact]
    public void GetHandHeldByIMEI_ShouldReturn_HandHeldWrapperDataView()
    {
        //Arrange
        var fixture = new Fixture();
        var imei = "imeiNo";
        var handHeldWrapperDataViewMoq = fixture.Create<HandHeldSummaryWrapperDataView>();
        mediatorMoq.Setup(x => x.Send(new GetHandHeldByIMEI(imei))).Returns(handHeldWrapperDataViewMoq);

        //Act
        ??

        //Assert
          ??
    }
}

}

K.Z
  • 5,201
  • 25
  • 104
  • 240

2 Answers2

4

According to error you have, you need to mock the CancellationToken which is an optional argument:

mediatorMoq.Setup(x => x.Send(new GetHandHeldByIMEI(imei), 
    It.IsAny<CancellationToken>())).Returns(handHeldWrapperDataViewMoq);
Mohsen Esmailpour
  • 11,224
  • 3
  • 45
  • 66
Stam
  • 2,410
  • 6
  • 32
  • 58
  • In addition the `new GetHandHeldByIMEI(imei)` will fail at run time as they will most likely have a different instance. better to use `It.*` there as well. But I guess they will cross that bridge when they eventually come across it. – Nkosi Jul 09 '21 at 12:49
0
[Fact]
public void GetHandHeldByIMEI_ShouldReturn_HandHeldWrapperDataView()
{
 //Arrange
   var fixture = new Fixture();
   var imei = "imeiNo";
   var handHeldWrapperDataViewMoq = fixture.Create<HandHeldWrapperDataView>();
        mediatorMoq.Setup(x => x.Send(new GetHandHeldByIMEI(imei), It.IsAny<CancellationToken>())).Returns(Task.FromResult(handHeldWrapperDataViewMoq));

    //Act
    var actualResult = sut.GetHandHeldByIMEI(imei);

    //Assert
       
 }
K.Z
  • 5,201
  • 25
  • 104
  • 240