【发布时间】:2011-03-11 12:31:38
【问题描述】:
这是我在 ASP.NET MVC 项目中的一个控制器使用 NUnit 和 Moq 进行的单元测试:
[Test]
public void Create_job_with_modelstate_errors_fails()
{
var job = new JobDto();
this.controller.ModelState.AddModelError("", "");
ActionResult result = this.controller.Create(job);
this.jobService.Verify(p => p.SaveJob(It.IsAny<JobDto>()), Times.Never());
// some other asserts removed for brevity
}
这很好用,但从维护的角度来看,我认为这行比它需要的更冗长:
this.postService.Verify(p => p.SavePost(It.IsAny<PostDto>()), Times.Never());
我真正想做的是等价于...
this.postService.VerifyNoMethodsCalled();
...我感兴趣的是我的控制器没有调用服务上的任何方法。这可以使用 Moq 吗?
【问题讨论】:
-
Google 搜索者:如果您有兴趣验证是否只调用了 SINGLE、PARTICULAR 方法,这就是您想要的问题:stackoverflow.com/questions/537308/…
标签: unit-testing mocking moq