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
I am finding problem when i try to rethrow the exception from the catch block is there any way to mock this.

public void somemethod()
{
try
{
// Call some methods
}
catch(Exception ex)
{
throw new CustomException(ex.message);s
}
}
asked by praveenrajr (600 points)

1 Answer

0 votes
Hi

You can use the Mock.ExpectAndThrow method.
This will let throw any exception you want when your method is called.

        public void Test()
        {
            Mock mock = MockManager.Mock(typeof(MyClass));
            mock.ExpectAndThrow("somemethod", new CustomException("My message"));

            try
            {
                MyClass.somemethod();
                Assert.Fail("should have throw an exception");
            }
            catch (CustomException e)
            {
                Assert.AreEqual("My message", e.Message);
            }
        }


Note that if you are using natural mocks you should use
the RecordExpectations.ExpectAndThrow or RecordExpectations.Throw methods
answered by ohad (35.5k points)
...