Thursday, July 22, 2010

First things first?

Clearly if I want to play with F# & ASP.NET MVC I should get the Visual Studio 2010 project template from Tomas Petricek. Then after installing in Visual Studio I get a new option to create a F# MVC Web Application. Mostly it just works but the reference in WebApplication.Core to the WebApplication.Data project needs to be updated for it to build. BTW, if you're looking for a quickstart C# MVC project check out Rob Conery's Tekpub MVC 2.0 Starter, it has an associated starter video available free from Tekpub. This is not that.

So what about MSpec? The latest release is available from TeamCity. I'm using ReSharper 5.1 so installing MSpec is just a matter of unblocking zip file (aargh), unzipping, pathing to unzipped location and running the appropriate batch file - & '.\InstallResharperRunner.5.1 - VS2010.bat'  (why are we still using batch files).

Now I have the pieces of the puzzle but I'm not entirely sure how I'm supposed to assemble them. Using a post from Elegant Code as a starting point I added a test project to my solution and copied Jan's sample into UnitTest1.cs. Visual Studio doesn't know what is [Subject("Making a customer preferred")] which is resolved by first creating a "libs" folder in my solution folder and adding Machine.Specifications.dll to it, then adding a reference to that dll to my test project. Then in the interests of seeing some actual output I need to flesh out the sample code with sufficient code to allow the test to compile:

1 [Subject("Making a customer preferred")]
2 public class when_a_regular_customer_is_made_preferred
3 {
4     Establish context = () =>
5     {
6        _order = new Order(new[] { new OrderItem(12),
7            new OrderItem(16) });
8        _totalAmountWithoutDiscount = _order.TotalAmount;
9
10         SUT = new Customer(new[] { _order });
11    };
12
13 Because of = () =>
14     SUT.MakePreferred();
15
16 It should_mark_the_customer_as_preferred = () =>
17     SUT.IsPreferred.ShouldBeTrue();
18
19 It should_apply_a_ten_percent_discount_to_all_outstanding_orders = () =>
20     _order.TotalAmount.ShouldEqual(_totalAmountWithoutDiscount * 0.9);
21
22     private static Customer SUT;
23
24     private static Order _order;
25     private static Double _totalAmountWithoutDiscount;
26 }
27
28 //
29 // Subject under test
30 //
31 public class Customer
32 {
33     private readonly List<Order> _orders;
34     public Boolean IsPreferred { get; private set; }
35
36    public Customer(IEnumerable<Order> orders)
37     {
38         _orders = new List<Order>(orders);
39     }
40
41 public void MakePreferred()
42 {
43     if (IsPreferred)
44         return;
45
46    IsPreferred = true;
47     _orders.ForEach(order => order.ApplyDiscount(10));
48 }
49 }
50
51 public class Order
52 {
53     private OrderItem[] orderItems;
54
55     public Order(OrderItem[] orderItems)
56     {
57         // TODO: Complete member initialization
58         this.orderItems = orderItems;
59     }
60     public double TotalAmount { get; set; }
61     public double ApplyDiscount(int percentDiscount)
62     {
63         return TotalAmount*(100 - percentDiscount)/100;
64     }
65
66 }
67
68 public class OrderItem
69 {
70     private int p;
71
72     public OrderItem(int p)
73     {
74         // TODO: Complete member initialization
75         this.p = p;
76     }
77 }

Now I can run the unit test using ReSharper and sure enough I get the correct output:
Making a customer preferred, when a regular customer is made preferred
    » should mark the customer as preferred
    » should apply a ten percent discount to all outstanding orders
Obviously this is a contrived example but I have established that I can run MSpec tests in my solution. If at some point it no longer works I know it's my bad.

1 comment:

Blogger said...

Are you looking to get cash from your visitors by using popup ads?
In case you do, did you know about PropellerAds?