這是從我在ASP.NET MVC項目控制器中的一個單元測試,使用NUnit和起訂量:起訂量 - 確認沒有方法被稱爲
[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嗎?
谷歌搜索:如果你有興趣驗證只是一個單一的,特定的方法不叫,這是你想要的問題,而不是:http://stackoverflow.com/questions/537308/how-to-verify-that-method-was-not-called-in-moq – 2016-02-11 20:14:16