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
We are trying to catch an OracleException in our business layer that has been rethrown by our Data Access Layer. We want to test that the exception is being caught properly, so we'd like to mock a thrown exception. Something like this:

            using (RecordExpectations recorder = RecorderManager.StartRecording())
            {
                ExampleDalClass mockedDal = new ExampleDalClass ();
                recorder.ExpectAndThrow(mockedDal.Insert(someData), new OracleException());
            }


However, this doesn't work because OracleException doesn't have any constructors. This will not even build.

Any suggestions?
________
Arizona Dispensary
asked by brauks (1.6k points)

2 Answers

0 votes
Hi,

You can use the MockObject class to create a fake OracleException which you can use as your return value. here is an example on how to do it:

reflective:
MockObject mockex = MockManager.MockObject<OracleException>(Constructor.NotMocked,"lior",6);
OracleException ex = mockex.Object as OracleException;


natural:
OracleException ex2 = RecorderManager.CreateMockedObject<OracleException>(Constructor.NotMocked,"lior",6);


In any case look herefor some info regarding usage of MockObjects
answered by lior (13.2k points)
0 votes
This worked great. Thanks!
________
Ford Excursion
answered by brauks (1.6k points)
...