简体   繁体   English

C# 从模拟对象的方法中捕获返回值

[英]C# Capturing the Returned Value from a Mocked Object's Method

I have the following C# method:我有以下 C# 方法:

public class MyClass {

   public bool foo(Dependency dependency)
   {
       bool result = false;
            
       var metadata = dependency.dependencyMethod();
            
       if(metadata.containsProperty()){
           // doSomething
       } else
       {
           // doSomethingElse
       }

       return result;
   }
}

I'd like to capture the value that gets returned by the dependency.dependencyMethod() (which is essentially the value of the method's metadata variable).我想捕获dependency.dependencyMethod()返回的值(本质上是方法的metadata变量的值)。

Basically, I'd like to assert that the value returned from dependency.dependencyMethod() matches the mocked response value I defined for it (described below):基本上,我想断言从dependency.dependencyMethod()返回的值与我为它定义的模拟响应值匹配(如下所述):

//Arrange
MyClass myClass = new MyClass(); // System Under Test
Mock<Dependency> dependencyMock = new Mock<Dependency>(); //Mocking dependency

// Mocking the behavior of the dependencyMock to return a pre-defined value when invoked
dependencyMock
    .Setup(d => d.dependencyMethod())
    .Returns("dummyMetadata");

// How do we capture the value returned when `dependencyMock.dependencyMethod()` gets invoked?
dependencyMock
    .Setup(d => d.dependencyMethod()) // When invoking the `dependencyMethod()`
    .Callback(); // <--- How to Capture the value returned when d.dependencyMethod() gets called, and assert that it matches "dummyMetaData"?

// Act
myClass.foo(dependencyMock);

Is there any way I can capture the value that gets returned from dependencyMock.dependencyMethod() ?有什么办法可以捕获从dependencyMock.dependencyMethod()返回的值吗?

Yes, the return value of dependencyMock.dependencyMethod() is a mocked one.是的, dependencyMock.dependencyMethod()的返回值是一个模拟值。 Regardless, I'd like to capture this returned value, and confirm that the pre-defined mocked response is, in fact, being returned.无论如何,我想捕获这个返回值,并确认预定义的模拟响应实际上正在返回。

You have to pass dependencyMock.Object to the foo method.您必须将dependencyMock.Object传递给 foo 方法。 You can verify the method wan called with您可以验证 wan 调用的方法

dependencyMock.Verify(mock => mock.dependencyMethod(), Times.Once()); 

It will always return the value that you set in the它将始终返回您在

dependencyMock.Setup(d => d.dependencyMethod()).Returns("dummyMetadata"); 

line.线。 In your act step call some method that uses the passed in " dummyMetadata " value, and verify that.在您的操作步骤中,调用一些使用传入的“ dummyMetadata ”值的方法,并进行验证。 Or you can mock something from which the dependencyMethod() calculates it return value and check that.或者您可以模拟dependencyMethod()从中计算返回值的东西并检查它。

In your unit test call dependencyMock.dependencyMethod() and assert that the return value equals "dummyMetadata".在您的单元测试中调用 dependencyMock.dependencyMethod() 并断言返回值等于“dummyMetadata”。 Put "dummyMetadata" in a variable or constant so you can refer to it both places.将“dummyMetadata”放入变量或常量中,以便您可以在两个地方引用它。

You are testing your Mock setup at this point so the above will verify you have it setup as desired.此时您正在测试您的 Mock 设置,因此上面的内容将验证您是否已根据需要进行设置。 It doesn't make any sense to try and test the return value inside your class under test MyClass as that is just proving Mock works properly.在测试 MyClass 下尝试测试 class 中的返回值没有任何意义,因为这只是证明 Mock 正常工作。 If you want to verify one time that the value returned inside MyClass is what is mocked just use the debugger to verify.如果您想验证一次 MyClass 中返回的值是被模拟的值,只需使用调试器进行验证。

I would argue that this assertion is not necessary but it is your choice to do if you want.我认为这个断言是没有必要的,但如果你愿意,你可以选择这样做。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM