Making the Magic happen.



Full code and dependencies here :


In this series of blog posts i would like to illustrate a number of techniques myself and the teams i work with regularly use to produce high quality software that delivers to the end user requirements.

The teams i work with and put together tend to come from the agile side of the software development camp. I will blog in coming weeks about requirement gathering and interviewing clients conducting workshops and other more ‘business’ focused techniques but i would like to start of with some development focused posts.

In the next 2 posts we will be using:


Enough talk , more action …

Below we have a simple example of a story which has 2 acceptance criteria defined in the remainder of this post we will cover the shallow dive. In the second post we will then dive deeper implementing the service layer and the persistence layer.


A quick note on tools we are using visual studio 2010 and Resharper , trying to do this sort of work with out Resharper is like trying to run a marathon with a broken leg you can do it but it will hurt lots and lots. This is so true that when i do contract work i have my own personal licence just in case the client doesn’t have Resharper, Generally by the time the contract has come to an end the whole team is using Resharper, it is that good a tool.

Story :

As a HR Administrator

When a New Employee starts work

Then I Expect to be able to register there details on the HR system

Acceptance Criteria :

  1. As a HR Administrator

When i create a new record for a Employee

Then I Expect  to enter the following information

  • FullName
  • Date of birth
  • Address

2. As a HR Administrator

When have entered all the employees details

Then I want to press save

And I Expect to see the employee on the employee list view

Lets get straight to some code

As usual we start with a class library with no framework or references we will be building this example in a purely emergent fashion.


Ok we have introduced some intent into the test with the name of the class but let us remember it is up for grabs to rename later , the name we have currently chosen will suffice for now and will get us going , as we explore the requirements and intent. (Tenet: Its ok to be wrong, as long as what you decide is good enough to allow you to move forward).


image image

Next we add a operation to represent the intent of our test and decorate it with the Test attribute remembering to add the TestFixture Attribute to the class declaration. We then need to add our testing assemblies i have chosen to use NUnit and some of the NSpec extensions that have been rolled into the NBehave Project. (Tenet: You don’t need it till you are driven to it , wear blinkers, ignore your pre knowledge.)

image image

So now we start to get into the implementation itself so we next write the story adding reference to the StoryQ dll, Resharper helpfully highlights the unimplemented steps in red, so lets Alt-Enter on each and resolve it.


so using Resharper and IntiliJ Mappings we can click on each red unimplemented method and choose ‘create method’ option 2.

image image

We then have a method stub for each step in the story and we are now in a position were by clicking on Resharper ‘yellow and green test icon we can run the test and get our first set of output.


So having run the test we can see that all the steps are pending so now we need to move on and implement the steps, the framework knows that they are pending because of the ‘NotImplementedException’ which is thrown, you will recall Resharper but those in each method by default for us.


The next step is to conceptualize the UI , so lets set the ‘EmployeeName’ property on the ‘conceptualized EmployeeCreator’ form.


So using Alt-Enter on Resharper we add a field and tell the system we will be using a Mock of type ‘IEmployeeCreator’ so we now need to get our mocking library my personal favourite id Moq.


With the Moq reference in place and our interface created were ready to implement the property on the interface, and verify it gets set on the mock, making its behaviour explicit within the universe of the test.


Next we set up the property on the Mocked View so we can set it, following the intent of the step. (Hint: Calling ‘Object’ on a Mock(Moq) returns to you the real object and then you can call the properties and operations as if it was a concrete object which in turn will obey your setup directives.


Next we run our test and it complains that the interface ‘IEmployeeCreator’ is not public so we fix that and re-run.


Back in the IDE we get driven to code, in the next step we enter the employees address now in reality this would probably be broken down into country , state, zip code and so on for the sake of this contrived example we will keep it as a simple string as opposed to pushing through to a address object. Note on this is that even in a production environment we might find ourselves well served just to implement a simple string and then once we have the test passing refactor this to a Address object.



We can see above that Resharper is telling us that the address property is not implemented so again we alt-enter on the method name and Resharper implements a property for us.


Again we follow our work flow and run our tests , then we implement the next step, the Date Of Birth which again drives out another property leaving us with:


The piece that i have delayed showing you as i did not want to distract attention from the work flow, is that Moq has a really useful piece of functionality for stubbing properties. Now we have more than 1 property on the Mock of the IEmplyeeCreator interface we will take advantage of this functionality, to do this In our set up method i have now add the following line of code:


This effectively creates automatic properties on your Moq object for each property expressed on your interface. This is very useful when we don’t want to verify a property or express some expectation on the event that property is set or get.You can think of it in terms of short hand for a Stub.

So where are we, and what has our story have produced so far?

We have the concept of a view double by mocking the IEmplyeeCreator  Interface , we will use this to define the UI implementation object a little later. We have on this interface defined a number of data items that we need the user to set, we have ‘for now’ used a mock all properties method on the Mock to do this as we are not concerned about these properties at this point.

The Last mile

Lets move to the next step in the story and a re-read it and it is talking about a save button i decide im uncomfortable with that because it is not abstracted enough and is implying implementation which as ‘Story Author’ I don’t want to really think about, So i click on the operation name and press ‘F2’ and Resharper opens its rename dialog.


I rename the step like so :


So having changed the semantics i move on to the implementation,  there are a number of things i can do here but as i like to keep my implemented UI as thin as possible i decide to raise an event to trigger the save operation, this also allows me to abstract if i so desire later the save operation to a outside collaborator such as Model / ViewModel or manager/ controller.

Ok so this bit is a bit of a big step but its were we finally start to see a connectivity point into the wider system be introduced.

First of all as discussed above we raise a RequestSave Event and at this stage i decide there isn’t a good reason to do anything other than cast the form to a interface which represents the data only and pass it out as a Argument in the eventArgs, this may change later but remember were wearing blinkers and doing only what we must to implement the behaviour.


We set up an event handler on our Mock / stub (View double) as we have no need at this stage for an orchestrator like a controller or presenter or such , but again we may do later.



As you can see above we have finally introduced our adapter , and although this is a long post in a production environment this is about 10 minutes worth of coding effort, for a pair with knowledge of the technique.

Ok so now we must mock our adapter and we must bring in our data interface and build our EventArgs class.



With these pieces in place we can see that when we run our story we are nicely taken to the the final step.


To Recap
  • We have represented a Behaviour in a story
  • We have created a View Double
  • We have created the concept of the user data
  • We have created a action and a interaction between to members of the code domain the view and the adapter which is the entry point to our larger system
  • We have one step out standing before we can get a green passing bar and start driving deeper into the system via the adapter.
The final Step in our shallow dive



We now set up our expectation in our final step again our entry point is the form double and in this case we are using the NBehave Extensions. Note this is a slightly modified copy of the extensions that i worked on with Agile Workshops Ronnie Barker, they can be downloaded from : or you can use the production NBehave extension form the NBehave Org.


This leads use to implement another event on the form double.


With this full collaborative exchange with the Adapter , i now feel driven to implement this object as such our next step is to create a concrete adapter. We let Resharper do the heavy work by adding the interface to the implementation list of the new class, and pressing alt-enter on the red squiggly line.

Once this is implemented we can go back and remove our service adapter mocks and use the concrete class. Principal Mocks are not always for life  , quite often they allow us to move forward till our design has emerged from our intent enough to demand a concrete implementation of the object.

This drives us to a decision point in our test intent originally we had this step which we had satisfied with a step declaration like so:


So in simple terms we wanted to know when the record had been saved in the service , so after going back to the story [ And potentially the Story Author / BA ]it is now plain to us that actually we don’t care about this step , what we care about is that when we ask for the list of employees we want to see the employee details in it. So we can safely remove this step. Principal don’t panic to much about your understanding of the story at the start the understanding will deepen and emerge as you move forward with the implementation of the story.


So as we can see above we now have a concrete Service adapter which as you will see in a moment leads me to implement a concrete employee object which is also visible in the above screen shot.



So if we re run our tests having implemented the concrete adapter we get to our NotImplmentedException as you can see our behaviour is driving us each step of the way.



So lets implement these method and get back to a clean state.



The next thing we do we raise the RequestEmployeeList event on our View Double.


Next we implement the handler for the RequestEmployeeList event and we call the GetEmployeeList from the now Concrete Service adapter.

This then drives us after running the tests to implement an internal storage in the adapter like so :


This then leads us to run our tests and we get yes a Green Bar thus we have completed our shallow dive between the form double and the client side end point the adapter.


I fully appreciate this is a long post but as stated above in a production environment with Resharper and keyboard/short cut skills this is no more than 10 – 15 minutes of work.

With out these skills and tools this is a lot longer normally measured in hours.

I hope this has been helpful. Next time we will look at implementing the View Double as a winform application using Unity IOC.

Full code and dependencies here :

About Beth Martin
software developer

2 Responses to Making the Magic happen.

  1. Cool post. The only gripe is whether it would read better using nsubstitute which would remove the need for Moq use of .Object. I like the use of StoryQ, something that Matt has been advocating here.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: