c# - 最小起订量异常 : Expected invocation on the mock once, 但为 0 次... - .Net Core 3.1 with xUnit

标签 c# asp.net-core moq xunit

我是 Moq 的新手,我正在尝试实现 2 个检查 HttpStatusCode.NotModified 的测试。和 HttpStatusCode.Ok .然而,后者通过了它的测试,但前者没有并返回异常:

Moq.MockException : Expected invocation on the mock once, but was 0 times: x => x.UpdateStateAsync(It.Is(y => y == RedisServiceModel))



这是HttpStatusCode.Ok通过:
        [Fact]
        public void UpdateState_StateHasBeenUpdated_HttpOk()
        {
            //arrange
            var state = new RedisServiceModel();
            var redisServiceMock = new Mock<IRedisService>();
            redisServiceMock.Setup(x => x.UpdateStateAsync(It.Is<RedisServiceModel>(y => y == state))).ReturnsAsync(state);
            var testController = new TestController(redisServiceMock.Object);

            // act
            var statusResult = testController.UpdateStates(state);

            // assert
            redisServiceMock.Verify(x => x.UpdateStateAsync(It.Is<RedisServiceModel>(y => y == state)));
            Assert.True(statusResult.Result is HttpStatusCode.OK);
        }

这是HttpStatusCode.NotModified抛出异常:
        [Fact]
        public void UpdateState_StateHasNotBeenModified_HttpNotModified()
        {
            //arrange
            var state = new RedisServiceModel();
            var redisServiceMock = new Mock<IRedisService>();
            redisServiceMock.Setup(x => x.UpdateStateAsync(It.Is<RedisServiceModel>(y => y == state))).ReturnsAsync(state);
            var testController = new TestController(redisServiceMock.Object);

            // act
            var statusResult = testController.UpdateStates(null);

            // assert
            redisServiceMock.Verify(x => x.UpdateStateAsync(It.Is<RedisServiceModel>(y => y == state)), Times.Once);
            Assert.True(statusResult.Result is HttpStatusCode.NotModified);
        }

这是PUT api调用:
        [HttpPut]
        [Route("updatestates")]
        public async Task<HttpStatusCode> UpdateStates(RedisServiceModel update)
        {
            RedisServiceModel updateState = await _redisService.UpdateStateAsync(update);

            if (updateState != null)
            {
                return HttpStatusCode.OK;
            }

            return HttpStatusCode.NotModified;
        }

我猜是因为我正在传入 null这里testController.UpdateStates(null) .我确实尝试将所有内容都包装在 UpdateStates 中api 方法到 null检查 update争论,但这仍然产生了同样的异常(exception)。如果需要更多代码,请告诉我,我会编辑这篇文章。

最佳答案

应该等待主体,否则可能在主体完成预期行为之前做出断言

模拟还应配置为返回传递的参数以模拟实际实现的预期行为

[Fact]
public async Task UpdateState_StateHasNotBeenModified_HttpNotModified() {
    //arrange
    var state = new RedisServiceModel();
    var redisServiceMock = new Mock<IRedisService>();
    redisServiceMock
        .Setup(x => x.UpdateStateAsync(It.IsAny<RedisServiceModel>()))
        .ReturnsAsync(x => x);
    var testController = new TestController(redisServiceMock.Object);

    // act
    var statusResult = await testController.UpdateStates(null);

    // assert
    redisServiceMock.Verify(x => x.UpdateStateAsync(It.Is<RedisServiceModel>(y => y == null)), Times.Once);
    Assert.True(statusResult is HttpStatusCode.NotModified);
}

其他测试也应该这样做
[Fact]
public async Task UpdateState_StateHasBeenUpdated_HttpOk()
{
    //arrange
    var state = new RedisServiceModel();
    var redisServiceMock = new Mock<IRedisService>();
    redisServiceMock
        .Setup(x => x.UpdateStateAsync(It.Is<RedisServiceModel>()))
        .ReturnsAsync(x => x);
    var testController = new TestController(redisServiceMock.Object);

    // act
    var statusResult = await testController.UpdateStates(state);

    // assert
    redisServiceMock.Verify(x => x.UpdateStateAsync(It.Is<RedisServiceModel>(y => y == state)));
    Assert.True(statusResult is HttpStatusCode.OK);
}

也就是说,应该重构主题操作以遵循 Controller 的正确语法。

假设 Controller 来自 ControllerBase它可以访问操作结果的辅助方法。
[HttpPut]
[Route("updatestates")]
public async Task<IActionResult> UpdateStates(RedisServiceModel update) {
    RedisServiceModel updateState = await _redisService.UpdateStateAsync(update);

    if (updateState != null) {
        return Ok();
    }

    return StausCode((int)HttpStatusCode.NotModified);
}

关于c# - 最小起订量异常 : Expected invocation on the mock once, 但为 0 次... - .Net Core 3.1 with xUnit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59668171/

相关文章:

c# - 如何覆盖 List.Add 方法?

c# - ASP.NET Core docker 构建错误

c# - 自动夹具 + AutoMoq : Create mock with excluded property

c# - 如何从 asp.net 核心服务访问 DbContext

jQuery 在 asp net core 2 razor 中设置日期值

c# - 将最小起订量与 Linq Any() 结合使用

c# - 模拟服务调用对象返回 null

c# - 获取传递给函数的参数的名称

c# - 找不到方法 : '!! 0 [] System.Array.Empty ()'

C# 随机浮点闭区间