NBehave + NSpec


So, quickly, I decided to go ahead and throw NSpec into the mix on top of NBehave.  Why not!  🙂

Here is what the example from my previous post would look like using NSpec:

[Test, Story]
        public void Load_checkout_information()
        {
            CheckoutOrderDTO checkoutOrderDTO = new CheckoutOrderDTO();
            ShoppingCartDTO shoppingCartDTO = new ShoppingCartDTO();
            IList<LookupDTO> listOfCreditCards = new List<LookupDTO>();
        Story story = <span style="color: #0000ff">new</span> Story(<span style="color: #006080">"Load checkout information"</span>);

        story
            .AsA(<span style="color: #006080">"controller that is responsible for performing the checkout process"</span>)
            .IWant(<span style="color: #006080">"to load the checkout information based on the current user's pending order"</span>)
            .SoThat(<span style="color: #006080">"I can edit the customer's information before submitting the order"</span>);

        story
            .WithScenario(<span style="color: #006080">"Order is ready to be checked out"</span>)

                .Given(<span style="color: #006080">"controller is prepared"</span>, <span style="color: #0000ff">delegate</span> { PrepareController(controller, <span style="color: #006080">"checkout"</span>, <span style="color: #006080">"index"</span>); })
                    .And(<span style="color: #006080">"pending order is retrieved from checkout service"</span>,
                        <span style="color: #0000ff">delegate</span> { Expect.Call(mockCheckoutService.GetOrderForCheckout()).Return(checkoutOrderDTO); })
                    .And(<span style="color: #006080">"shopping cart is retrieved from shopping cart service"</span>,
                        <span style="color: #0000ff">delegate</span> { Expect.Call(mockBasketService.GetShoppingCart()).Return(shoppingCartDTO); })
                    .And(<span style="color: #006080">"list of acceptable credit card types are retrieved from credit card lookup service"</span>,
                        <span style="color: #0000ff">delegate</span> { Expect.Call(mockCreditCardLookupService.GetAllCreditCardTypes()).Return(listOfCreditCards); })
                    .And(<span style="color: #006080">"mock object framework has stopped recording"</span>, <span style="color: #0000ff">delegate</span> { mockery.ReplayAll(); })

                .When(<span style="color: #006080">"the index action is executed"</span>, <span style="color: #0000ff">delegate</span> { controller.Index(); })

                .Then(<span style="color: #006080">"the pending order should exist in the property bag with key of"</span>, PB.CheckoutOrder,
                        <span style="color: #0000ff">delegate</span>(<span style="color: #0000ff">string</span> key) { Specify.That(controller.PropertyBag[key]).ShouldEqual(checkoutOrderDTO); })
                    .And(<span style="color: #006080">"the shopping cart should exist in the property bag with key of"</span>, PB.ShoppingCart,
                        <span style="color: #0000ff">delegate</span>(<span style="color: #0000ff">string</span> key) { Specify.That(controller.PropertyBag[key]).ShouldEqual(shoppingCartDTO); })
                    .And(<span style="color: #006080">"the list of accepted credit card types should exist in the property bag with key of"</span>, PB.AllCreditCardTypes,
                        <span style="color: #0000ff">delegate</span>(<span style="color: #0000ff">string</span> key) { Specify.That(controller.PropertyBag[key]).ShouldEqual(listOfCreditCards); });
    }</pre>

 

Of course I know that the use of NSpec here is basically just syntactic sugar over the Assert statement, but it definitely reads better.

← NBehave Experiment: MonoRail Controllers &#038; Rhino Mocks
My First Interview (that I have given) →