Mocking the behaviour of operators in Moq C#

橙三吉。 提交于 2020-03-22 09:21:51

问题


Hope your well.

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

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.

Your time is appreciated

Thanks


回答1:


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)
}


来源:https://stackoverflow.com/questions/11898447/mocking-the-behaviour-of-operators-in-moq-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!