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
0 votes
Hallo,

I want to change the WhenCalled behaviour after some time during my test. Unfortunately typemock (7.4.0.0) everytime uses the first assignment.
Ihave the following testcase:
- My method does some data manipulation, but only if called first time.
- the manipulated data is in space and will be send to a web service.
- If the web service is not availible or throws an exception I have to call it again but without the data manipulation.
- I want to verify that the correct manipulated data is send to the webservice.

For this I made this test:
[Test]
public void CloseCaseExecute_FirstTimeExceptionSecondTImeOk_ValueEpanded()
{
Data data = null;
SetValidator();
Isolate.WhenCalled(() => webservice.AnyCall(null, false)).WillThrow(new Exception(""));
viewModel.Lastname = "abc";

// The last name is epanded by AP.
CloseCaseAndWait();

Isolate.WhenCalled(() => webservice.AnyCall(null, false)).DoInstead(x => data = ((Data)x.Parameters[0]));

//Verification if its still "AP last name" and not e.g. "AP AP lastnáme".
//But the web service call throws still an exception.
CloseCaseAndWait();

Assert.AreEqual("AP abc", viewModel.Lastname);
}

thanks for any reply and I would be happy to find a solution.
asked by Moritz (3.3k points)

2 Answers

0 votes
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.
answered by alex (17k points)
0 votes
ah ok, it s so easy :)
answered by Moritz (3.3k points)
...