Hi Moritz,
You should use
Sequenced method calls in order to solve this issue.
Sequenced method calls are basically the ability to write a sequence of "WhenCall"s on the same method of an object and make it behave according the sequence during the run:
The 1st WhenCalled sets the behavior of the 1st call,
The 2nd WhenCalled sets the behavior of the 2nd call,
...
The last WhenCalled sets the behavior of 'n'th call and on until the end of the test
See the example below:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
//ARRANGE
var foo = new Foo();
Isolate.WhenCalled( ()=>foo.DoSomething()).WillThrow(new Exception());
Isolate.WhenCalled( ()=>foo.DoSomething()).WillReturn(10);
//ACT
var result = foo.Bar();
//ASSERT
Assert.AreEqual(10, result);
}
}
public class Foo
{
public int Bar()
{
try
{
DoSomething();
return 0;
}
catch (Exception e)
{
return DoSomething();
}
}
public int DoSomething()
{
return 5;
}
}
*Note that it is recommended no to break the
AAA structure of the test.