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
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)
{
}
}
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
??
}
}
}
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);