测试用例失败

问题描述 投票:0回答:1

执行测试用例时,我收到此错误:

消息: Moq.MockException:
预期在模拟上调用一次,但实际调用次数为 0 次:m => m.Map(AnnotationMasterRequest)

执行的调用:MockIMapper:1(m):
未执行任何调用。

服务方式:

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

测试用例:

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

地图绘制者:

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

        CreateMap<Annotation, AnnotationResponse>();
    }
}

我尝试了这段代码,我希望测试用例能够成功运行。

c# .net moq xunit
1个回答
0
投票

需要将函数捕获为参数并调用,以便执行其表达式主体。

...

_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);

...

假设测试安排了必要的成员以允许调用的函数执行完成。

© www.soinside.com 2019 - 2024. All rights reserved.