Created
September 8, 2012 14:41
-
-
Save davybrion/3675532 to your computer and use it in GitHub Desktop.
Code snippets for "Mocking expensive template methods" post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract class Command | |
| { | |
| private readonly List<string> errorMessages = new List<string>(); | |
| public void Execute() | |
| { | |
| try | |
| { | |
| CheckAuthorization(); | |
| CheckErrorMessages(); | |
| ValidateInput(); | |
| CheckErrorMessages(); | |
| ProcessInput(); | |
| CheckErrorMessages(); | |
| } | |
| catch (Exception e) | |
| { | |
| Logger.Log(e); | |
| // do something clever that developers supposedly don't think of | |
| // ... | |
| } | |
| } | |
| protected virtual void CheckAuthorization() { } | |
| protected virtual void ValidateInput() { } | |
| protected virtual void ProcessInput() {} | |
| protected void AddErrorMessage(string errorMessage) | |
| { | |
| errorMessages.Add(errorMessage); | |
| } | |
| private void CheckErrorMessages() | |
| { | |
| if (errorMessages.Count > 0) | |
| { | |
| // throw some kind of exception which contains all of the messages | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class DeleteCustomerCommand : Command | |
| { | |
| private Customer customer; | |
| private CustomerDataLayerComponent customerDAL; | |
| public void Execute(Customer customer) | |
| { | |
| this.customer = customer; | |
| customerDAL = new CustomerDataLayerComponent(); | |
| Execute(); | |
| } | |
| protected override void CheckAuthorization() | |
| { | |
| if (!AuthorizationManager.IsAllowedToAccess<DeleteCustomerCommand>()) | |
| { | |
| AddErrorMessage("sorry buddy, no access"); | |
| } | |
| } | |
| protected override void ValidateInput() | |
| { | |
| if (customerDAL.GetOutstandingOrderCount(customer.Id) > 0) | |
| { | |
| AddErrorMessage("Customer can't be deleted when there are outstanding orders"); | |
| } | |
| } | |
| protected override void ProcessInput() | |
| { | |
| try | |
| { | |
| customerDAL.Delete(customer); | |
| } | |
| catch (Exception e) | |
| { | |
| AddErrorMessage(e.Message); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| customerDAL = new CustomerDataLayerComponent(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if (!AuthorizationManager.IsAllowedToAccess<DeleteCustomerCommand>()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [Test] | |
| public void AddsErrorMessageWhenUserDoesntHaveAccessToThisCommand() | |
| { | |
| var mocks = new MockRepository(); | |
| var command = mocks.PartialMock<DeleteCustomerCommand>(); | |
| PrepareAuthorizationManagerToDenyAccess(); | |
| command.Stub(c => c.ValidateInput()).Do(new Action(EmptyMethodThatDoesntDoAnyting)); | |
| command.Stub(c => c.ProcessInput()).Do(new Action(EmptyMethodThatDoesntDoAnyting)); | |
| mocks.ReplayAll(); | |
| AssertThrows<Exception>("sorry buddy, no access", () => command.Execute(new Customer())); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [Test] | |
| public void AddsErrorMessageWhenCustomerHasOutstandingOrders() | |
| { | |
| var mocks = new MockRepository(); | |
| var command = mocks.PartialMock<DeleteCustomerCommand>(); | |
| var customer = new Customer(); | |
| CreateOutstandingOrderFor(customer); | |
| command.Stub(c => c.CheckAuthorization()).Do(new Action(EmptyMethodThatDoesntDoAnyting)); | |
| command.Stub(c => c.ProcessInput()).Do(new Action(EmptyMethodThatDoesntDoAnyting)); | |
| mocks.ReplayAll(); | |
| AssertThrows<Exception>("Customer can't be deleted when there are outstanding orders", | |
| () => command.Execute(customer)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment