Skip to content

Instantly share code, notes, and snippets.

@davybrion
Created September 8, 2012 14:41
Show Gist options
  • Select an option

  • Save davybrion/3675532 to your computer and use it in GitHub Desktop.

Select an option

Save davybrion/3675532 to your computer and use it in GitHub Desktop.
Code snippets for "Mocking expensive template methods" post
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
}
}
}
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);
}
}
}
customerDAL = new CustomerDataLayerComponent();
if (!AuthorizationManager.IsAllowedToAccess<DeleteCustomerCommand>())
[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()));
}
[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