Hi,
I was wondering if the following exception throwing implementation is intended.
public class ObjectUnderTest
{
public void MethodUnderTest()
{
}
}
public class DerivedException : Exception
{
public DerivedException()
: base()
{
}
public DerivedException(string message)
: base(message)
{
}
}
[TestFixture]
public class TestHarness
{
[Test]
[ExpectedException(typeof(Exception))]
public void WorkingExceptionTest()
{
// Create the object to be tested
ObjectUnderTest obj = new ObjectUnderTest();
// Start mocking
using (RecordExpectations recorder = new RecordExpectations())
{
obj.MethodUnderTest();
recorder.Throw(new Exception("TypeMock test"));
}
// Invoke method
obj.MethodUnderTest();
// Fails if exception not thrown
Assert.Fail("Exception not thrown");
}
[Test]
public void FailingExceptionTest()
{
// Create the object to be tested
ObjectUnderTest obj = new ObjectUnderTest();
// Start mocking
using (RecordExpectations recorder = new RecordExpectations())
{
obj.MethodUnderTest();
// COMMENT
// I think that the DerivedException creation is being mocked???
recorder.Throw(new DerivedException("TypeMock test"));
}
// Invoke method
obj.MethodUnderTest();
// Fails if exception not thrown
// TEST FAILS HERE
Assert.Fail("Exception not thrown");
}
}
The first test passes as per the documentation and provided examples :)
The second test creates a derived exception to be returned but this is not being thrown when the method is invoked. The failure assertion is being generated. I suspect that the creation of the derived exception may be mocked but this is a guess.
Could you please shed some light on this issue?
Thanks
Paul