验证的次保护的方法是使用Moq的被叫号码 [英] Verify the number of times a protected method is called using Moq

查看:223
本文介绍了验证的次保护的方法是使用Moq的被叫号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的单元测试,我用嘲笑起订量受保护的方法,并想断言,它被称为若干倍。 <一href="http://stackoverflow.com/questions/390632/verifying-protected-abstract-methods-are-called-using-moq">This问题描述相似,起订量的早期版本的内容:

  //期望ChildMethod1()将被调用一次。 (它的保护)
testBaseMock.Protected()。期待(ChildMethod1)
  .AtMostOnce()
  .Verifiable();

...
testBase.Verify();
 

但这无法再正常工作;自那时以来,语法发生了变化,我无法找到新的等效利用起订量4.x的:

  testBaseMock.Protected()。设置(ChildMethod1)
  //没有AtMostOnce()或相关方法了
  .Verifiable();

...
testBase.Verify();
 

解决方案

在的 Moq.Protected 命名空间,有一个的 IProtectedMock 接口,有一个验证方法,以时间为参数。<​​/ P>

修改 这是因为至少起订量4.0.10827可用。语法例如:

  testBaseMock.Protected()设置(ChildMethod1);

...
。testBaseMock.Protected()验证(ChildMethod1,Times.Once());
 

In my unit-tests I'm mocking a protected method using Moq, and would like to assert that it is called a certain number of times. This question describes something similar for an earlier version of Moq:

//expect that ChildMethod1() will be called once. (it's protected)
testBaseMock.Protected().Expect("ChildMethod1")
  .AtMostOnce()
  .Verifiable();

...
testBase.Verify();

but this no longer works; the syntax has changed since then and I cannot find the new equivalent using Moq 4.x:

testBaseMock.Protected().Setup("ChildMethod1")
  // no AtMostOnce() or related method anymore
  .Verifiable();

...
testBase.Verify();

解决方案

In the Moq.Protected namespace, there is an IProtectedMock interface that has a Verify method taking Times as a parameter.

Edit This is available since at least Moq 4.0.10827. Syntax example:

testBaseMock.Protected().Setup("ChildMethod1");

...
testBaseMock.Protected().Verify("ChildMethod1", Times.Once());

这篇关于验证的次保护的方法是使用Moq的被叫号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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