gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:52:36 52 4
gpt4 key购买 nike

我是 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/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com