English 中文(简体)
Moq - 核实没有使用任何方法
原标题:Moq - verify that no methods were called

这是我的一个控制者在伙伴关系中进行的一次单位测试。 NET MVC Project, using theNUnit and 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?

最佳回答

你可以与MockBehavior一道创建Mock。 Strict, e.g.

this.postService = new Mock<IPostService>(MockBehavior.Strict);

这样,如果你不预示任何期望,任何要求都是如此。 员额 服务失败

问题回答

Modern answer (Moq 4.8 or later):

mock.VerifyNoOtherCalls();

这种方法确保除以前经核实的电话外不发出任何电话。 在本案中,没有收到<代码>mock.Verification(......)说明。 因此,它将确保永远不会有人叫.。

如果有人呼吁:

This mock failed verification due to the following unverified invocations:
...

这并不要求严格限制模拟。

资料来源:Moq快速启动方案/a>。





相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签