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 have a method which, when called with certain bad parameters, should first call private method "A", and then should catch an exception thrown by "A", then call "A" again with other parameters.

I'd like to let the first call "go through", but to intercept the second call and make sure the parameters are correct.

Please point me in the right direction on how to do this, as I'm a new user of TypeMock.
asked by JohnWSaundersIII (760 points)

3 Answers

0 votes
Hi,

In order to solve this scenario we will use sequenced calls to WhenCalled API.
You can find more information about sequencing here.

Please look at this example:

    public class Boo
    {
        public void A(string var1)
        {
        }

        public void MethodCallingA()
        {
            try
            {
                this.A(null);
            }
            catch (Exception e)
            {
                this.A("goodString");
                
            }
        }
    }


    [TestClass, Isolated]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            Boo boo  = new Boo();
            
            Isolate.WhenCalled(() => boo.A(null)).WillThrow(new Exception("first Exception"));
            Isolate.WhenCalled(() => boo.A("goodString")).WithExactArguments().CallOriginal();

            boo.MethodCallingA();

            Isolate.Verify.WasCalledWithExactArguments(() => boo.A("goodString"));

        }

   }


In the test method we set A's behavior so the first call will throw an exception, and the second call will happen only if the parameters are as expected.
The Verify checks if the second call actually happened. If the parameters in the second call was not as expected, the test will fail.
answered by NofarC (4k points)
0 votes
Thanks for the answer.

I see that I should have specified that this is for a non-public (private) method, so WithExactArguments can't be used.

Is there another technique that will work for non-public methods?
answered by JohnWSaundersIII (760 points)
0 votes
Hi John,

You mentioned that the method is private and we should have seen it.

In this case the solution is the same only the syntax is different:

[TestClass, Isolated]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            Boo boo = new Boo();

            Isolate.NonPublic.WhenCalled(boo, "A").WillThrow(new Exception("first Exception"));
            Isolate.NonPublic.WhenCalled(boo, "A").CallOriginal();

            boo.MethodCallingA();

            Isolate.Verify.NonPublic.WasCalled(boo, "A").WithArguments("GoodString");
        }
    }


    public class Boo
    {
        private void A(string var1)
        {
        }

        public void MethodCallingA()
        {
            try
            {
                this.A(null);
            }
            catch (Exception e)
            {
                this.A("GoodString");
            }
        }
    }



May I ask what is it that you are trying to test?
Is it A("GoodString") or is it the code that comes after that in the catch block?
answered by alex (17k points)
...