Skip to content

Instantly share code, notes, and snippets.

@davybrion
Created September 3, 2012 18:50
Show Gist options
  • Select an option

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

Select an option

Save davybrion/3612196 to your computer and use it in GitHub Desktop.
code snippets for "Testing Exceptions" post
public class Order
{
public IEnumerable<OrderLine> OrderLines { get; set; }
public Customer Customer { get; set; }
public Order() : this(null, null) {}
public Order(Customer customer) : this(customer, null) {}
public Order(Customer customer, IEnumerable<OrderLine> orderLines)
{
Customer = customer;
OrderLines = orderLines;
}
public decimal CalculateTotal()
{
return OrderLines.Sum(o => o.Price) * Customer.DiscountRate;
}
}
public enum OrderValidationProblem
{
OrderLinesNotSet,
OrderLinesHasNoItems,
CustomerNotSet
}
public class OrderValidationException : Exception
{
public OrderValidationProblem Problem { get; set; }
public OrderValidationException(OrderValidationProblem problem)
{
Problem = problem;
}
}
[TestFixture]
public class OrderValidationTests
{
[Test]
[ExpectedException(typeof(OrderValidationException))]
public void CustomerNotSetThrowsValidationException()
{
var order = new Order();
order.OrderLines = EntityTestFactory.CreateDummyOrderLines();
order.CalculateTotal();
}
[Test]
[ExpectedException(typeof(OrderValidationException))]
public void OrderLinesNotSetThrowsValidationException()
{
var order = new Order(new Customer());
order.CalculateTotal();
}
[Test]
[ExpectedException(typeof(OrderValidationException))]
public void OrderLinesWithNoItemsThrowsValidationException()
{
var order = new Order(new Customer(), new OrderLine[] {});
order.CalculateTotal();
}
}
public decimal CalculateTotal()
{
if (OrderLines == null) throw new OrderValidationException(OrderValidationProblem.OrderLinesNotSet);
if (OrderLines.Count() == 0) throw new OrderValidationException(OrderValidationProblem.OrderLinesHasNoItems);
if (Customer == null) throw new OrderValidationException(OrderValidationProblem.CustomerNotSet);
return OrderLines.Sum(o => o.Price) * Customer.DiscountRate;
}
private void CheckExceptionAndProblem(Func<Decimal> function,
OrderValidationProblem expectedOrderValidationProblem)
{
try
{
function();
}
catch (OrderValidationException e)
{
Assert.AreEqual(expectedOrderValidationProblem, e.Problem);
return;
}
Assert.Fail("Exception was not thrown");
}
[Test]
public void CustomerNotSetThrowsValidationException()
{
var order = new Order();
order.OrderLines = EntityTestFactory.CreateDummyOrderLines();
CheckExceptionAndProblem(order.CalculateTotal, OrderValidationProblem.CustomerNotSet);
}
[Test]
public void OrderLinesNotSetThrowsValidationException()
{
var order = new Order(new Customer());
CheckExceptionAndProblem(order.CalculateTotal, OrderValidationProblem.OrderLinesNotSet);
}
[Test]
public void OrderLinesWithNoItemsThrowsValidationException()
{
var order = new Order(new Customer(), new OrderLine[] {});
CheckExceptionAndProblem(order.CalculateTotal, OrderValidationProblem.OrderLinesHasNoItems);
}
private T GetThrownException<T>(Action code) where T : Exception
{
try
{
code();
}
catch (T expectedException)
{
return expectedException;
}
catch (Exception e) {}
Assert.Fail("Expected exception of type {0} was not thrown", typeof(T).FullName);
return null;
}
private void CheckExceptionAndProblem(Func<Decimal> function,
OrderValidationProblem expectedOrderValidationProblem)
{
var expectedException = GetThrownException<OrderValidationException>(() => function());
Assert.AreEqual(expectedOrderValidationProblem, expectedException.Problem);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment