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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
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
asked by Paul (1.2k points)

1 Answer

0 votes
Hi Paul
You are right the derived exception is mocked.
The should create the derived exception outside the 'using' block
like this:

        [Test]
        [ExpectedException(typeof(DerivedException))]
        public void FailingExceptionTest()
        {
            // Create the object to be tested 
            ObjectUnderTest obj = new ObjectUnderTest();
            //Create the exception outside the using block - will not be mocked!
            DerivedException ex = new DerivedException("TypeMock test");
            // Start mocking 
            using (RecordExpectations recorder = new RecordExpectations())
            {
                obj.MethodUnderTest();
                // COMMENT 
                // I think that the DerivedException creation is being mocked??? 
                recorder.Throw(ex);
            }

            // Invoke method 
            obj.MethodUnderTest();

            // Fails if exception not thrown 
            // TEST FAILS HERE 
            Assert.Fail("Exception not thrown");
        }
answered by ohad (35.4k points)
...