简体   繁体   English

如何在Rhino Mock'd对象中调用回调?

[英]How can I call a callback in a Rhino Mock'd object?

I know there has to be a way to do this...I'm just new to RhinoMocking. 我知道必须有一种方法...我只是RhinoMocking的新手。

Essentially, I have the following class: 本质上,我有以下课程:

class B
{
     private object _dependency;

     public B(object dependency)
     {
          _dependency = dependency;
     }

     public void Method()
     {
         dependency.DependencyMethod(delegate(){ Method2(); });
     }

     private void Method2()
     {
         // do stuff
     }
}

I'm passing a rhino mocked, interface as a dependency in my test. 我在测试中通过了一个犀牛模拟接口作为依赖项。 I want to test Method2 in my test...but its private, so the only way to get at it is by looking at the argument to DependencyMethod. 我想在我的测试中测试Method2 ...但是它是私有的,所以唯一的方法是查看DependencyMethod的参数。 How do I do that? 我怎么做? :P :P

Thanks! 谢谢!

One test should determine that Method() passes a delegate. 一个测试应该确定Method()通过了一个委托。 Another test should determine that the real dependency calls the delegate. 另一个测试应该确定真正的依赖项调用了委托。

To to verify that Method() passes the delegate to Method2 you need to make the delegate visible. 若要验证Method()将委托传递给Method2,您需要使委托可见。 Perhaps you can pass this as a dependency so that in another test you can verify that Method() passes you the delegate that you passed in. 也许您可以将此作为依赖项传递,以便在另一个测试中可以验证Method()是否将您传递的委托传递给您。

 private object _dependency;
 private delegate_type _delegate;

 public B(object dependency, delegate_type theDelegate)
 {
      _dependency = dependency;
      _delegate= theDelegate;
 }

 public B(object dependency) : this(dependency, delegate(){ Method2(); }) {}

 public void Method()
 {
     dependency.DependencyMethod(theDelegate);
 }

因此,在深入研究之后,我认为最好的方法是在我的模拟游戏中使用GetArgumentsForCallsMadeOn,以这种方式获取委托,然后调用它。

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

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