简体   繁体   English

Moq测试method1是否在同一类中调用第二个method2

[英]Moq testing if method1 calls second method2 in same class

I'm new to Moq 我是Moq的新手

I read a lot about Moq testing and that you shouldn't test your mock object, instead you should use Moq to stub dependencies and make it to act like you want to. 我了解了很多有关Moq测试的知识,并且您不应该测试自己的模拟对象,而应该使用Moq来对依赖项进行存根并使其表现为所需的行为。 But now I am asking myself, how can you test if a method be called from another method in the same class, like this code: 但是现在我问自己,如何测试是否从同一类的另一个方法中调用一个方法,例如以下代码:

public class A // --> class i want to test
{
  public virtual void TestMethod() // --> does it call testmethod2
  {
    TestMethod2();
  }

  public virtual void TestMethod2()
  {
    // Do something
  }
}

I thought I can write the Unittest like this: 我以为我可以这样写单元测试:

[TestMethod]
public void MyTestMethod()
{
  Mock<A> aMock = new Mock<A>();
  aMock.Verify(o => o.TestMethod2(), Times.Once);

  aMock.TestMethod();

  aMock.VerifyAll();
}

But is this valid? 但这有效吗? Thank you for all good answers! 感谢您的所有好的答案!

Moq is only able to mock out methods which are virtual (or provide mock implementations for interfaces). Moq仅能模拟出虚拟方法(或为接口提供模拟实现)。 If you try a Setup or Verify on a non-virtual member, you'll get the error 如果您尝试在非虚拟成员上进行SetupVerify ,则会收到错误消息

Invalid verify on a non-virtual member: m => m.TestMethod2() 对非虚拟成员的无效验证:m => m.TestMethod2()

In your case, you would need to change TestMethod2 to virtual, ie 在您的情况下,您需要将TestMethod2更改为virtual,即

public virtual void TestMethod2()

Which can then be tested: 然后可以对其进行测试:

var aMock = new Mock<A>();

aMock.Object.TestMethod();

aMock.Verify(m => m.TestMethod2(), Times.Once);

As you've suggested, testing whether a class calls methods internal to itself is a smell, indication that you're either 如您所建议,测试类是否在其内部调用方法是一种气味,表明您是

  • Testing internal implementation detail (eg if TestMethod2() was private, it's none of our business to even know about it). 测试内部实现细节(例如,如果TestMethod2()是私有的,那么甚至不知道它也不关我们的事)。
  • Or, an indication that the Class / System under test is in need of refactoring into multiple classes, which should be loosely coupled, and hence can be isolated and better testable. 或者,表明正在测试的类/系统需要重构为多个类,这些类应该松散耦合,因此可以被隔离并且可以更好地测试。

Note 注意

  • You can't Verify a call before you've invoked the method on the SUT that you want to test, ie move the Verify to after the invocation of .TestMethod() as I've done. 在调用要测试的SUT上的方法之前,您无法Verify调用,即,在我完成.TestMethod()调用之后,将Verify移至。
  • Although you could change your code to use the Setup + VerifyAll approach to testing, ie ensuring that everything you've setup is actually invoked: 尽管您可以更改代码以使用Setup + VerifyAll方法进行测试,即确保已实际调用您设置的所有内容:

aMock.Setup(m => m.TestMethod2());

aMock.Object.TestMethod();

aMock.VerifyAll();

There is however debate around whether the usage of VerifyAll violates AAA principals (since you're also in effect specifying the Verify criteria in the Arrange piece), and I've also found that this approach makes it impossible to refactor and DRY up Mock setups in large scale tests, and you also lose some of the finer level of checking (eg Times.Once ). 但是,关于VerifyAll的使用是否违反AAA原则存在VerifyAll (因为您实际上还在Arrange部分中指定了Verify条件),而且我还发现,这种方法无法重构和干燥Mock设置。在大规模测试中,您还会失去一些更好的检查级别(例如Times.Once )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 方法method1和method2使用相同的SOAPAction - The methods method1 and method2 use the same SOAPAction 错误:&#39; <method1> &#39;和&#39; <method2> &#39;不能互相超载 - Error: '<method1>' and '<method2>' cannot overload each other c#在method1执行后从method2中的method1访问字符串 - c# accessing string from method1 in method2, after method1's execution Moq .net在类方法调用中 - Moq .net in class method calls 对于良好的编码惯例,如果我们已经在方法1中进行了验证,那么我们是否仍需要在方法2中再次验证数据?方法1将数据传递给方法2? - For Good coding practice, do we still have to validate data again in method2 if we already validated in method1 & method1 passes that data to method2? 在C#中调用method2之前,有什么方法可以强制调用method1 - Is there any way to make compulsion to call method1 before calling method2 in c# 如何在单元测试中使用Moq调用同一类中的另一个方法 - How to use Moq in unit test that calls another method in same class 在我的 C# 项目中,我想在特定条件下退出 Method1,并执行 Method2 而不再返回 Method1。 我怎样才能做到这一点? - In my C# project,I want to exit Method1 on a specific condition, and execute Method2 without coming back to Method1 again. How can I do that? Moq测试删除方法 - Moq testing Delete method 使用Moq对方法进行单元测试 - Unit testing a method with Moq
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM