chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
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.
asked by allon.guralnek (10.6k points)

3 Answers

0 votes
Hi Allon,

Well we have kind of a problem here, consider this:
1. We want to worn the user form calling WhenCalled() without a completing statement.
2. If the user Will call WhenCalled without the Isolator throwing exception the test would probably fail from the wrong reason.

What happened in your case is that the exception was thrown before WillReturn method run Isolator caught the exception and throw the exception with the warning that you saw.
The problem here is that we are guessing here and we can't be always right :(
answered by ohad (35.5k points)
0 votes
Can't you include the current exception as the InnerException and let the user decide what's relevant? For example, your exception could look like:

*** Cannot use WhenCalled without a complementing behavior statement. Make sure that WhenCalled has a chained method specifying the behavior and that the following exception that was thrown did not prevent the chained method from being called:

System.Exception: "You forgot to frizzle the baz"
    at MyNamespace.Foo.ctor()....
    at ......
    at ......
answered by allon.guralnek (10.6k points)
0 votes
Hi Allon,

Agree with you here, I'm adding it as a bug to our feature list.
answered by ohad (35.5k points)
...