views:

171

answers:

1

I'm a little confused as to the purpose of the ExpectConstructor() method on the Mock class in TypeMock 3.5.

I would have thought that calling ExpectConstructor would cause the MockManager to fail upon Verify() if the constructor is not called, i.e. if an instance of the mocked type is not instantiated.

However, calling ExpectConstructor() without specifying any arguments to that constructor doesn't appear to set that expectation - and so my test passes regardless.

My questions: Am I missing or misunderstanding something? If ExpectConstructor() isn't for verifying a constructor call, what's it for?

Consider these three NUnit tests which illustrate the problem:

[Test]
public void exampleTest1()
{
   MockManager.Init();
   Mock fooMock = MockManager.Mock(typeof(Foo));
   fooMock.ExpectConstructor().Args(10);

   Foo f = new Foo(10);   // Constructor called
   MockManager.Verify();  

   // This test passes, as expected...so far so good
}

[Test]
public void exampleTest2()
{
   MockManager.Init();
   Mock fooMock = MockManager.Mock(typeof(Foo));
   fooMock.ExpectConstructor();

   Foo f = new Foo();  // Constructor called
   MockManager.Verify();  

   // This test passes...also as expected
}

[Test]
public void exampleTest3()
{
   MockManager.Init();
   Mock fooMock = MockManager.Mock(typeof(Foo));
   fooMock.ExpectConstructor();

   // nb. not instantiating an instance of Foo

   MockManager.Verify();  

   // This test passes - why?!
}

Thanks in advance for your help.

+2  A: 

This was reported as a bug back in 2006. Supposedly it was going to be fixed (well, implemented - apparently it wasn't a bug, it was just an unimplemented feature...) but if it's still a problem now, I guess they didn't get round to it :(

Jon Skeet
Great - thanks. They may well have fixed this in a later version...unfortunately, for the time being I'm stuck with the bug. Thanks again.
sgreeve