Created
May 10, 2012 19:51
-
-
Save danielwertheim/2655438 to your computer and use it in GitHub Desktop.
Some approvals fiddling
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 static class JsonApprovals | |
| { | |
| public static void VerifyAsJson<T>(T item) | |
| { | |
| VerifyJson(item.ToJson()); //Using extension method ToJson from ServiceStack.Text | |
| } | |
| public static void VerifyJson(string json) | |
| { | |
| Approvals.Verify(new ApprovalTextWriter(json.Dump(), "txt")); //Using extension method Dump from ServiceStack.Text | |
| } | |
| } |
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 ShoppingCart | |
| { | |
| public Dictionary<string, int> Contents = new Dictionary<string, int>(); | |
| public void Put(string article, int qty) | |
| { | |
| if (Contents.ContainsKey(article)) | |
| Contents[article] += qty; | |
| else | |
| Contents.Add(article, qty); | |
| } | |
| } | |
| public class Order | |
| { | |
| public string OrderNo { get; set; } | |
| public DateTime Date { get; set; } | |
| public Dictionary<string, OrderLine> Lines { get; set; } | |
| public Order() | |
| { | |
| Lines = new Dictionary<string, OrderLine>(); | |
| } | |
| } | |
| public class OrderLine | |
| { | |
| public string ArticleNo { get; set; } | |
| public int Qty { get; set; } | |
| public int UnitPrice { get; set; } | |
| } | |
| public class OrderService | |
| { | |
| public Order Initiate(ShoppingCart cart) | |
| { | |
| var order = new Order(); | |
| order.Date = DateTime.Now; | |
| order.OrderNo = Guid.NewGuid().ToString(); | |
| order.Lines = cart.Contents.Select(pair => new OrderLine { ArticleNo = pair.Key, Qty = pair.Value }).ToDictionary(pair => pair.ArticleNo, pair => pair); | |
| return order; | |
| } | |
| } |
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] | |
| [UseReporter(typeof(DiffReporter))] | |
| public class When_adding_bananas_and_milk_to_a_cart | |
| { | |
| [Test] | |
| public void It_should_have_bananas_and_milk() | |
| { | |
| var cart = new ShoppingCart(); | |
| cart.Put("Bananas", 2); | |
| cart.Put("Milk", 1); | |
| JsonApprovals.VerifyAsJson(cart.Contents); | |
| } | |
| } | |
| [TestFixture] | |
| [UseReporter(typeof(DiffReporter))] | |
| public class When_turning_a_cart_with_bananas_and_milk_to_an_order | |
| { | |
| [Test] | |
| public void It_should_also_have_bananas_and_milk() | |
| { | |
| var cart = new ShoppingCart(); | |
| cart.Put("Bananas", 2); | |
| cart.Put("Milk", 1); | |
| var order = new OrderService().Initiate(cart); | |
| JsonApprovals.VerifyAsJson(order.Lines); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment