在Moq C#中模拟运算符的行为 [英] Mocking the behaviour of operators in Moq C#

查看:93
本文介绍了在Moq C#中模拟运算符的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

祝你健康.

我正在使用C#中的Moq创建一些测试.我要嘲笑的对象之一已覆盖==,>和<运营商.

I am in the process of creating some tests using Moq in C#. One of the objects I am Mocking has overridden ==, > and < operators.

有人知道它是否可行,以及如何...配置一个Mock对象来复制它.我问的原因是,我试图注入一个模拟存根,因为我得到了一些具有深层和肮脏依赖性的遗留代码.

Does anyone know if its possible, and if so how to... configure a Mock object to replicate this. The reason I ask is that I am trying to inject a mocked stub as some legacy code I have been given that has deep and dirty dependencies.

感谢您的时间

谢谢

推荐答案

当您覆盖此类操作时,您还应该提供其命名的等效项.如果您以这种方式重做代码,则模拟起来会更容易.

When you override such operations you should also provide theirs named equivalents too. If you rework yours code in such way it will be easier to mock it.

public static bool operator ==(SomeType a, SomeType b)
{
    return a.Equals(b);
}

public virtual bool Equals(SomeType b)
{
   // yours logic here
   return base.Equals(b)
}

这篇关于在Moq C#中模拟运算符的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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