- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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);
}
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/
为什么Intellij会给出这个警告,这是什么意思,我怎样才能让它变得更好? import akka.actor.Props object Router { def props(config: C
我在两个类中使用react-native-image picker和react-native-image-crop picker库。 一个是启动库图像(react-native-image picke
晚上好, 我刚开始使用 Microsoft.Contracts(最新版本)并将其插入示例界面之上,现在它看起来像这样: namespace iRMA2.Core.Interfaces { us
我尝试制作用于 Symfony 2 上传的 ajax 脚本。Chrome 返回此错误: Uncaught TypeError: Illegal invocation jquery.min.js:4 我
我正在尝试在 java 中运行一些 javascript。我不确定脚本是否正确,我想在一段时间后终止调用。这就是我运行脚本的方式。 returnMethod = invocableEngine.inv
我正在为一个服务类编写 Spock 测试,该服务类的方法在 spring boot 应用程序中调用 Dao 类中的另一个方法。但是,我得到: Too few invocations for: 1
我们正在运行一个 Create React App (CRA) Web 应用程序,我们已向其中添加了 Google Analytics v4。我们使用 ga-4-react 启动分析npm 包。 in
我想将多个图像对象发布到 testphp.php。但控制台打印错误说非法调用。 我已经尝试过: submit $("#sub").click(function(){ // get th
当文本框为空时尝试禁用按钮时,我在 google chrome 控制台中收到此错误: function isEmpty() { var r = document.getElementById;
这是 html 这是脚本 $('#submit').click(function() { var files = $("[type='file']")[0].fil
我尝试通过ajax提交表单,下面是表单。 Date Upload File
我想弄乱 Speech Recognition API,所以我从简单的页面开始,该页面在单击 body 元素时开始识别。我的 scripts.js 文件是: var recognition = new
我在 Google 上进行了搜索,也浏览了 Stack 上提供的一系列类似问题,但不幸的是没有一个能帮助我解决问题。 下面是我正在做的事情,我从主视图调用 PartialView,但我的 Partia
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
为简单起见,我们来看一个非常简单的类: public class TestingClass { public void method1(){ System.out.printl
编辑:到目前为止建议的答案都不起作用。 我正在使用 django 运行此调用。第一次运行时,服务器返回“n_usr”(这会更改用户文件的形式)。第二次,它只是抛出一个非法调用错误。 function
每当我必须使用 gcloud 部署新的 python 函数时sdk 我收到这条消息 Allow unauthenticated invocations of new function [functio
这个问题已经有答案了: getUserMedia() in JavaScript normalizes across browsers. Illegal Invocation (1 个回答) 已关闭
我试图通过发布一些 xml 来登录来测试登录 Web 服务,但它再次出现此错误: 未捕获的类型错误:非法调用 这是代码: $(document).ready(function() { var da
function submit() { console.log('Submit!'); } function foo(callback, param) { console.log(ca
我是一名优秀的程序员,十分优秀!