I'm not sure if this is a problem with TypeMock or the way Microsoft handles instrumentation of assemblies, but since the problem disappears when I disable TypeMock, I'll report it here.
The problem was first seen in a test for some parts of our production code. However, to simplify matters I have reproduced the problem in the sample code below (i.e. the code is pointless, but it does illustrate the problem):
namespace UnitTestProblems {
public class MockProblems {
public static bool DoSomething() {
try {
return Helper.Help();
} catch (MyException e) {
Console.WriteLine(e);
return false;
}
}
}
public class Helper {
public static bool Help() {
throw new MyException();
}
}
public class MyException : Exception {
}
}
[TestMethod]
public void TestMethod() {
Assert.IsFalse(MockProblems.DoSomething());
}
I'm perfectly aware that the above code doesn't use any TypeMock features. The test project includes a reference to TypeMock though.
However, if TypeMock and instrumentation is enabled MyException isn't caught in the catch block. The code will fail with an unhandled exception of type MyException?! I.e. there's something completely wrong here, cause the exception should have been caught by the catch block.
If TypeMock or instrumentation is disabled, everything works fine.
The problem was encountered when we used TypeMock to throw the exception, but as the code shows it can be reproduced without using TypeMock for this.
Similarly, if the catch block is modified as shown below
} catch (Exception ee) {
MyException e = ee as MyException;
Console.WriteLine(e);
return false;
}
everything is fine as well.
If MyException is move to another assembly the problem goes away as well.
As the problem cannot be produced without the presence of TypeMock, I take it that the chaining of instrumentation and TypeMock is somehow faulty and I would appreciate any input on the matter.
Regards,
Brian