Created
September 3, 2012 18:50
-
-
Save davybrion/3612196 to your computer and use it in GitHub Desktop.
code snippets for "Testing Exceptions" 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 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; | |
| } | |
| } |
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 enum OrderValidationProblem | |
| { | |
| OrderLinesNotSet, | |
| OrderLinesHasNoItems, | |
| CustomerNotSet | |
| } |
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 OrderValidationException : Exception | |
| { | |
| public OrderValidationProblem Problem { get; set; } | |
| public OrderValidationException(OrderValidationProblem problem) | |
| { | |
| Problem = problem; | |
| } | |
| } |
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
| [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(); | |
| } | |
| } |
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 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; | |
| } |
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
| 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"); | |
| } |
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 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); | |
| } |
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
| 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; | |
| } |
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
| 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