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.