Below is my service method
public async Task<ServiceResponse<AnnotationMasterResponse>> CreateAnnotation(AnnotationMasterRequest annotationMasterRequest)
{
return await serviceResponseExceptionHandler.HandleAsync(async () =>
{
var annotationMaster = _mapper.Map<AnnotationMaster>(annotationMasterRequest);
annotationMaster.AnnotationMasterId = Guid.NewGuid();
var responseResult = await base.Insert(annotationMaster);
return responseResult.DataResult;
}, annotationMasterRequest);
}
Test Case for above method
[Fact]
public async Task CreateAnnotation_Success()
{
// Arrange
var annotationRequest = new AnnotationMasterRequest
{
AnnotationMasterName = "test"
};
var annotationMaster = new AnnotationMaster
{
AnnotationMasterName = annotationRequest.AnnotationMasterName,
};
var annotationResponse = new AnnotationMasterResponse { };
var expectedResponse = new ServiceResponse<AnnotationMasterResponse>
{
Success = true,
HttpStatusCode = HttpStatusCode.OK,
DataResult = annotationResponse
};
_mapperMock.Setup(m => m.Map<AnnotationMaster>(It.IsAny<AnnotationMasterRequest>()))
.Callback<object>(req => Console.WriteLine($"Mapper called with: {req}"))
.Returns(annotationMaster);
_baseRepositoryMock.Setup(repo => repo.Insert(It.IsAny<AnnotationMaster>())).ReturnsAsync(true); // or false, depending on the test
_exceptionHandlerMock.Setup(h => h.HandleAsync(It.IsAny<Func<Task<AnnotationMasterResponse>>>(), annotationRequest)).ReturnsAsync(expectedResponse);
var result = await _annotationMasterService.CreateAnnotation(annotationRequest);
// Assert
Assert.NotNull(result);
Assert.True(result.Success);
Assert.Equal(HttpStatusCode.OK, result.HttpStatusCode);
Assert.NotNull(result.DataResult);
_mapperMock.Verify(m => m.Map<AnnotationMaster>(It.Is<AnnotationMasterRequest>(req => req.AnnotationMasterName == annotationRequest.AnnotationMasterName)), Times.Once);
}
Getting error at _mapperMock.Verify
Moq.MockException: ' Expected invocation on the mock once, but was 0 times: m => m.Map(It.Is(req => req.AnnotationMasterName == annotationRequest.AnnotationMasterName))