mercredi 25 mai 2016

Calling same method multiple times with different returns using MOQ

I am writing a unit test case where I need to rollback the first payment when the first payment is a success and the second payment fails. The unit test code looks like the following:

  1. Process first payment successfully.
  2. Process second payment. External system will throw similar transaction fault exception. Parse similar transaction fault exception to get exceptionid and process second transaction again, using the exception id.
  3. When the second transaction is actually processed, the system returns a "Payment declined fault exception".
  4. The system should then void the first payment.

I have written the unit test for (1). How SO community have pointers on how to take care of part 2, 3, 4?

    [TestMethod]    
        public void IfTheSecondPaymentFailsThenTheFirstPaymentShouldBeVoided()
        {
            var iPaymentMock = new Mock<IPaymentMock>();
            var paymentSpecificationResponse = new PreregisteredAccountSpec();    
            iPaymentMock.Setup(
                    counter => counter.ProcessPayment
                        (
                            It.IsAny<Context>(),
                            It.IsAny<PreregisteredAccountSpec>(),  
                            It.IsAny<Guid>())
                        ).
                    Returns(paymentSpecificationResponse);  
 iPaymentMock.Verify((
                counter => counter.ProcessPaymentSpecification
                (
                    It.IsAny<Context>(),
                    It.IsAny<PreregisteredAccountSpec>(),  
                    It.IsAny<Guid>()
                )
            ), Times.Once());  
        }

Aucun commentaire:

Enregistrer un commentaire