Wednesday, July 6, 2011

Configuring Moq to return different results

If you need to create a mock that can return one result the first time and another the second time, you can use the MoqExtensions class like this:

MockPremiumCalculationService = new Mock<IPremiumCalculationService> ();
PremiumCalculationResponse response1 = new PremiumCalculationResponse ();
response1.Premium = 55;
PremiumCalculationResponse response2 = new PremiumCalculationResponse ();
response2.Premium = 60;
MockPremiumCalculationService.Setup (
    pcs => pcs.Calculate (It.IsAny<PremiumCalculationRequest> ()))
    .ReturnsInOrder (response1, response2);

For more info see this article by Phil Haack:

http://haacked.com/archive/2009/09/29/moq-sequences.aspx

No comments:

Post a Comment