When an exception is thrown when evaluating the expression of the parameter of .WillReturn(), instead of the actual exception throw, a TypeMockException is thrown, making it incredibly difficult to located the issue, since an exception has been swallowed. Repro - given the following classes:
public class Foo
{
public Foo()
{
throw new Exception("You forgot to frizzle the baz");
}
}
public class ObjectMaker
{
public static object Make()
{
return new object();
}
}
The following test throws a TypeMockException with the error "*** Cannot use WhenCalled without a complementing behavior statement" instead of an Exception with "You forgot to frizzle the baz" which is essential to debug the issue.
[TestClass]
[Isolated]
public class CrazySingletonTests
{
[TestMethod]
public void Test1()
{
Isolate.WhenCalled(() => ObjectMaker.Make()).WillReturn(new Foo());
var actual = ObjectMaker.Make();
Assert.IsTrue(actual is Foo);
}
}
That test stumped me for quite some time because the actual error was being hidden from me. Only when I started decomposing the test was the actual error revealed. I was told that when Isolator reaches the end of a test and detects that WhenCalled has executed but WillReturn was not, as in the above example, it throws a TypeMockException. It should only do so if there isn't another exception already in the process of being thrown.