Setup Sequences - hypersolutions/hypermock GitHub Wiki

There are situations where you might want to call a mock several times with the same parameters and get different returns or responses. This is where sequences come in.

When you define your setup for a mock, you can chain returns and/or throws to the response, and each time the mocked action is called with the matching parameters, the next response in the chain is returned, For example:

[Fact]
public void SetupReturnsTrueThenFalseOnlySubsequentCalls()
{
    MockFor<IAccountService>().SetupGet(s => s.HasAccounts).Returns(true).Returns(false);

    Assert.True(Subject.HasAccounts());
    Assert.False(Subject.HasAccounts());
}

[Fact]
public void SetupThrowsExceptionOnSecondCall()
{
    MockFor<IAccountService>().SetupGet(s => s.HasAccounts).Returns(true).Throws(new Exception());

    Assert.True(Subject.HasAccounts());
    Assert.Throws<Exception>(() => Subject.HasAccounts());
}

The above shows a couple of examples. The first one shows that if the HasAccounts property is called twice, the first time it will return true and the second time it will return false.

In the second example, you can see that it will throw an exception a second time.

Note that these responses are cyclic meaning that if you were to call the HasAccounts above a third time, you would get the first response.

⚠️ **GitHub.com Fallback** ⚠️