验证是否使用 Moq 3.1 调用了基本受保护方法
我有一个继承自抽象基类的类。我试图验证基类中指定的受保护方法是否被调用两次,并且我想验证传递的参数是否是特定值(每次调用都不同)。
我希望能够将 Protected
与 Expect
或 Verify
一起使用,但似乎我错过了可以做什么这些方法。
我正在尝试的事情可以通过最小起订量实现吗?
更新: 我想做的一个例子:
class MyBase
{
protected void SomeMethodThatsAPainForUnitTesting(string var1, string var2)
{
//Stuf with file systems etc that's very hard to unit test
}
}
class ClassIWantToTest : MyBase
{
public void IWantToTestThisMethod()
{
var var1 = //some logic to build var 1
var var2 = //some logic to build var 2
SomeMethodThatsAPainForUnitTesting(var1, var);
}
}
本质上我想测试变量 var1 和 var2 正确创建并传递到 SomeMethodThatsAPainForUnitTesting
的方式,所以本质上我想模拟受保护的方法,验证它至少被调用一次并且参数全部正确传递。如果这是在接口上调用方法,那将是微不足道的,但我正在摆脱受保护的方法。
我无法轻易更改设计,因为它是棕地开发,而且我不是唯一调用该方法的类。
I have a class that inherits from an abstract base class. I am trying to verify that a specified protected method in the base class is called twice and I'd like to verify that the parameters passed are specific values (different for each call).
I was hoping that I'd be able to use Protected
with either Expect
or Verify
, but seemingly I've missed what can be done with these methods.
Is what I'm attempting possible with moq?
UPDATE:
An example of what I'm trying to do:
class MyBase
{
protected void SomeMethodThatsAPainForUnitTesting(string var1, string var2)
{
//Stuf with file systems etc that's very hard to unit test
}
}
class ClassIWantToTest : MyBase
{
public void IWantToTestThisMethod()
{
var var1 = //some logic to build var 1
var var2 = //some logic to build var 2
SomeMethodThatsAPainForUnitTesting(var1, var);
}
}
Essentially I want to test the way the variables var1 and var2 are created correctly and passed into SomeMethodThatsAPainForUnitTesting
, so essentially I want to mock out the protected method, verify that it was called at least once and that the parameters were all passed correctly. If this was calling a method on an interface it would be trivial, but I'm coming unstuck with a protected method.
I can't easily change the design as it's brown field development and I'm not the only class calling the method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这是不可能的。
Moq 和 Rhino 模拟等工具通过在运行时生成您想要模拟的类型的子类来发挥其魔力。他们通过重写成员(在虚拟成员的情况下)或实现它(在接口的情况下)并插入代码来检查或记录传递的参数并返回预定义的内容来生成所有“验证”和“期望”逻辑。罐头回复。
那么你能做些什么来解决这个问题呢?首先,如果您可以将基方法更改为虚拟方法,那么您可以通过创建一个测试工具来测试您的方法,如下所示:-
如果由于代码是如此可怕的棕地而根本无法更改基类,那么您就不需要如果不想弄脏鞋子,您可以简单地将方法作为受保护的虚拟方法包装在 ClassIWantToTest 中,并对其执行相同的技巧。 Moq 确实支持覆盖受保护的虚拟成员(请参阅此处的杂项部分) - 虽然我个人在这种情况下更喜欢手动测试工具。
No it's isn't possible.
Tools like Moq and Rhino mocks work their magic by generating subclasses of the types you want to mock at run time. They generate all their "Verify" and "Expect" logic by overriding a member (in the case of virtual members) or implementing it (in the case of an interface) and inserting code to inspect or record the passed arguments and return the pre-canned response.
So what can you do to get around this? Firstly if you could alter the base method to be virtual this would then allow you to test your method by creating a test harness like so:-
If it's not possible to alter the base class at all due to the code being so horribly brownfield you don't want to get your shoes dirty, you could simply wrap the method in ClassIWantToTest as a protected virtual method and perform the same trick on that instead. Moq does have support for overriding protected virtual members (see the miscellaneous section here) - though personally I prefer manual test harnesses in this case.