验证是否通过Moq调用了代表 [英] Verifying a delegate was called with Moq

查看:37
本文介绍了验证是否通过Moq调用了代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,该类通过参数获得一个委托. 此类调用该委托,我想用Moq对它进行单元测试. 我如何验证此方法是否被调用?

i got a class that gets by argument a delegate. This class invokes that delegate, and i want to unit test it with Moq. how do i verify that this method was called ?

示例类:

public delegate void Foo(int number);

public class A
{
   int a = 5;

   public A(Foo myFoo)
   {
      myFoo(a);
   }
}

,我想检查是否调用了Foo. 谢谢.

and I want to check that Foo was called. Thank you.

推荐答案

使用匿名函数怎么办?它可以像内联模拟一样,您不需要模拟框架.

What about using an anonymous function? It can act like an inline mock here, you don't need a mocking framework.

bool isDelegateCalled = false;
var a = new A(a => { isDelegateCalled = true});

//do something
Assert.True(isDelegateCalled);

这篇关于验证是否通过Moq调用了代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆