public void EmailCommandRollback_ExceptionThrow() { //arrange Int32 execCounter = 0; Int32 rollbackCounter = 0; //mock up the counters var mockEmailCommandPass = new Mock <ICommand <EmailCommandArgs> >(); mockEmailCommandPass.Setup(a => a.Execute(It.IsAny <EmailCommandArgs>())).Callback(() => { execCounter++; }); mockEmailCommandPass.Setup(a => a.RollBack(It.IsAny <EmailCommandArgs>())).Callback(() => { rollbackCounter++; }); var mockEmailCommandFail = new Mock <ICommand <EmailCommandArgs> >(); mockEmailCommandFail.Setup(a => a.Execute(It.IsAny <EmailCommandArgs>())).Callback <EmailCommandArgs>((a) => { execCounter++; //do not set the IsSuccessful flag, to false, that //should be taken care of within the exception handler throw new Exception(); }); mockEmailCommandFail.Setup(a => a.RollBack(It.IsAny <EmailCommandArgs>())).Callback(() => { rollbackCounter++; }); var chain = new ProcessInboxChain(); chain.AddCommand(mockEmailCommandPass.Object); chain.AddCommand(mockEmailCommandPass.Object); chain.AddCommand(mockEmailCommandPass.Object); chain.AddCommand(mockEmailCommandFail.Object); var args = new EmailCommandArgs(null, null, null, 0, false); //act chain.ExecuteChain(args); //assert mockEmailCommandFail.VerifyAll(); mockEmailCommandPass.VerifyAll(); Assert.IsFalse(args.IsSuccessful); Assert.IsTrue(execCounter == 4); Assert.IsTrue(rollbackCounter == 4); Assert.NotNull(args.Exception); }
public void EmailCommandRollback_LastCommandFailed_Test() { //arrange Int32 execCounter = 0; Int32 rollbackCounter = 0; //mock up the counters var mockEmailCommandPass = new Mock <ICommand <EmailCommandArgs> >(); mockEmailCommandPass.Setup(a => a.Execute(It.IsAny <EmailCommandArgs>())).Callback(() => { execCounter++; }); mockEmailCommandPass.Setup(a => a.RollBack(It.IsAny <EmailCommandArgs>())).Callback(() => { rollbackCounter++; }); var mockEmailCommandFail = new Mock <ICommand <EmailCommandArgs> >(); mockEmailCommandFail.Setup(a => a.Execute(It.IsAny <EmailCommandArgs>())).Callback <EmailCommandArgs>((a) => { a.IsSuccessful = false; execCounter++; }); mockEmailCommandFail.Setup(a => a.RollBack(It.IsAny <EmailCommandArgs>())).Callback(() => { rollbackCounter++; }); var chain = new ProcessInboxChain(); chain.AddCommand(mockEmailCommandPass.Object); chain.AddCommand(mockEmailCommandPass.Object); chain.AddCommand(mockEmailCommandPass.Object); chain.AddCommand(mockEmailCommandFail.Object); var args = new EmailCommandArgs(null, null, null, 0, false); //act chain.ExecuteChain(args); //assert mockEmailCommandFail.VerifyAll(); mockEmailCommandPass.VerifyAll(); Assert.IsFalse(args.IsSuccessful); Assert.IsTrue(execCounter == 4); Assert.IsTrue(rollbackCounter == 4); }