Constructing mocks in unit tests

Posted by Flynn1179 on Stack Overflow See other posts from Stack Overflow or by Flynn1179
Published on 2010-06-14T12:20:51Z Indexed on 2010/06/14 12:22 UTC
Read the original article Hit count: 238

Filed under:
|

Is there any way to have a mock constructed instead of a real instance when testing code that calls a constructor?

For example:

public class ClassToTest
{
  public void MethodToTest()
  {
    MyObject foo = new MyObject();
    Console.WriteLine(foo.ToString());
  }
}

In this example, I need to create a unit test that confirms that calling MethodToTest on an instance of ClassToTest will indeed output whatever the result of the ToString() method of a newly created instance of MyObject.

I can't see a way of realistically testing the 'ClassToTest' class in isolation; testing this method would actually test the 'myObject.ToString()' method as well as the MethodToTest method.

© Stack Overflow or respective owner

Related posts about c#

Related posts about nunit-mocks