Yash Depani
Yash Depani

Reputation: 23

Test case fails

When executing test case I'm getting this error:

Message: Moq.MockException:
Expected invocation on the mock once, but was 0 times: m => m.Map(AnnotationMasterRequest)

Performed invocations:MockIMapper:1 (m):
No invocations performed.

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:

public class AnnotationMasterServiceTests
{
    private readonly Mock<IUnitOfWork> _unitOfWorkMock;
    private readonly Mock<IMapper> _mapperMock;
    private readonly Mock<IServiceResponseExceptionHandler> _exceptionHandlerMock;
    private readonly AnnotationMasterService _service;

    public AnnotationMasterServiceTests()
    {
        _unitOfWorkMock = new Mock<IUnitOfWork>();
        _mapperMock = new Mock<IMapper>();
        _exceptionHandlerMock = new Mock<IServiceResponseExceptionHandler>();
        _service = new AnnotationMasterService(
            _unitOfWorkMock.Object,
            _mapperMock.Object,
            _exceptionHandlerMock.Object);
    }

    [Fact]
    public async Task CreateAnnotation_ShouldReturnSuccess_WhenValidRequest()
    {
        // Arrange
        var request = new AnnotationMasterRequest 
        {
            AnnotationMasterName = "test"
        };
        var annotationMaster = new AnnotationMaster();

        var annotationMasterResponse = new AnnotationMasterResponse { /* populate fields */ };

        // Create a mock ServiceResponse that returns a valid response, but we don't need to access internal properties.
        var serviceResponse = new ServiceResponse<AnnotationMasterResponse>
        {
            // Leave the internal properties (DataResult, Success, Message) as they are
            // Your test doesn't need to access them directly; we assume the ServiceResponse is set up correctly
        };

        _mapperMock.Setup(m => m.Map<AnnotationMaster>(request)).Returns(annotationMaster);

        _exceptionHandlerMock.Setup(e => e.HandleAsync(It.IsAny<Func<Task<AnnotationMasterResponse>>>(), request)).ReturnsAsync(serviceResponse);

        // Act
        var result = await _service.CreateAnnotation(request);

        // Assert
        Assert.NotNull(result);
        Assert.Equal(serviceResponse.DataResult, result.DataResult);
        _mapperMock.Verify(m => m.Map<AnnotationMaster>(request), Times.Once);
    }

Mapper:

public class AnnotationMasterMapper : Profile
{
    public AnnotationMasterMapper()
    {
        CreateMap<AnnotationMasterRequest, AnnotationMaster>();
        CreateMap<AnnotationMaster, AnnotationMasterResponse>();

        CreateMap<Annotation, AnnotationResponse>();
    }
}

I tried this code, and I am expecting the test case to run successfully.

Upvotes: 2

Views: 48

Answers (1)

Nkosi
Nkosi

Reputation: 247471

The function needs to be captured as an argument and invoked so that its expression body is executed.

...

_exceptionHandlerMock
    .Setup(e => e.HandleAsync(It.IsAny<Func<Task<AnnotationMasterResponse>>>(), It.IsAny<AnnotationMasterRequest>()))
    .Callback((Func<Task<AnnotationMasterResponse>> func, AnnotationMasterRequest req) => {
        func(); //<-- invoke the function
    })
    .ReturnsAsync(serviceResponse);

...

assuming that the test arranged the necessary members to allow the invoked function to be executed to completion.

Upvotes: 0

Related Questions