Hi,
We have some code which calls external code through .NET remoting. The external code returns an object and we then check if this object is an exception and throws a local exception accordingly.
I'm trying to test this code and I have mocked the call to the external code. Since I want to test the error handling in this particular test I mock the call with ExpectAndReturn and forces the method to return an object derived from Exception. However, for some reason TypeMock intercepts this and claims that the Exception was actually thrown. I.e. my error checking routine is never called.
The problem can be reproduced with the following small example code:
public class MyClass {
public object ReturnSomethingThatMayBeAnException(object returnthis) {
return returnthis;
}
}
[TestMethod]
public void TestMyClass() {
Mock mock = MockManager.Mock(typeof(MyClass));
mock.ExpectAndReturn("ReturnSomethingThatMayBeAnException", new Exception("Who threw this exception?"));
MyClass myclass = new MyClass();
object result = myclass.ReturnSomethingThatMayBeAnException(42);
Assert.IsTrue(result is Exception);
}
MyClass represents the external code and the function represents a remote call. If I force the function to return an Exception TypeMock claims that MyClass
threw an exception ("Test method Library.LibraryTest.TestMyClass threw exception: System.Exception: Who threw this exception?"), which it certainly didn't.
If I wanted to mock the situation where an exception was thrown I would have used ExpectAndThrow. I.e. it seems to me that ExpectAndReturn does the wrong thing in this case.