-1

I have some production code like

private bool IsTypeEqual(object theFirstObject, object theSecondObject)
{
       if(theFirstObject.GetType()==theSecondObject.GetType())
       {
               return true;
       }
       else
       {
                return false;
       }
}

Now i have to write the unit test case for this code. I am using NMock to create the object. So when i am passing the object of two different classes it should go to else part. But actually as i am mocking both the objects, so GetType() returning the MockObject type for both of the object. How can i solve this problem.

David Basarab
  • 72,212
  • 42
  • 129
  • 156
  • 2
    This doesn't have anything to do with the question why didn't you just do a return (theFirstObject.GetType() == theSecondObject.GetType()) – David Basarab Jul 22 '09 at 10:54
  • You can't mock an "object". It's not an interface or a abstract class. – Liam Oct 09 '15 at 08:06

2 Answers2

0

You dont need to mock "theFirstObject" or "theSecondObject". You really dont care what happens to these classes, you just need to assert the result is correct.

If I were you I would pass in different type and assert whether it is true/false:

Assert.AreEqual(false, IsTypeEqual("HelloWorld", 192));

Assert.AreEqual(true, IsTypeEqual("Hello", "World"));
David Kiff
  • 1,200
  • 2
  • 11
  • 24
-1

I hope this code will help you.

private bool IsTypeEqual<TMockedType>(object theFirstObject, object theSecondObject)
{
    Matcher matcher = Is.TypeOf(typeof(TMockedType));

    return matcher.Matches(theFirstObject) && matcher.Matches(theSecondObject);
}