2

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

Nick
  • 948
  • 2
  • 12
  • 24

1 Answers1

2

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)
}
user854301
  • 5,383
  • 3
  • 28
  • 37
  • Thanks for your input. In this instance I need to avoid touching the existing code at all which makes life very difficult. This is for business reasons as the code is safety related and so validated etc. EDIT: The original developer has supplied those versions in this exa mple I have found, thanks though normally that would be a very good answer. – Nick Aug 10 '12 at 09:11
  • Fortunatly looking into it that is how the original object is written so in this instance this will work. Thanks for your time. – Nick Aug 10 '12 at 09:19