Archive
The Art of Mocking – Part 1
Mocking frameworks are a great way to mock objects and to do isolation testing. It promotes the TDD (Test Drive Development philosophy) and use of SOLID principles in your design which in my opinion is a good principle of designing.
I did a post some time back on interfaces & TDD, have a read through if you haven’t to see why it is important to have interfaces and separation of concerns.
Now back to into mocking framework, following are some of the popular mocking frameworks for .net :-
I use the Moq framework for most of my projects and in this first part of the series I will walk you through real business scenario and some pain points of testing which will give us compelling reason to use one of these frameworks.
Lets assume that we are writing a piece of code which checks a credit history of an applicant against a web service and then determines whether the loan should be approved or not.On top of it let say the Global Credit Service is hosted (in cloud ) and provided by some financial institute or a federal government regulators, and you are a consumer of that service.Following diagram represents how the classes and dependencies stick together and we will see the code right after this.

As you can see the GlobalCreditServices is the service in the cloud and our code calls this service.
Lets start writing some code, first a class called CreditHistoryCheck which has method called “GetCreditHistory” and the method simply calls the webservice to get the credit history of the applicant.

Then we create another class LoanApprovalService which calls the GetCreditHistory method and based on the results it applies business rules.
( We are using the SRP single responsibility principle to create this class )

This is just a quick code we have written without thinking much about refactoring, SOLID principles or testing in our mind.
Lets see how the unit test looks like ( it’s actually an integration testing)

The problem I see with this code is that whenever we run this code we are hitting the actual service and we are not even sure whether it will return the correct data to test the business rule.
For example the business rules clearly states that if the person has a “Very Bad” credit rating and is of more that 45 years old and if doesn’t have equity more than $ 100,000 then the loan will be rejected. ( It’s just an example and wish I had come up with a better one). So we have to know in advance the ID or the social security number of the person who has a bad credit in order to pass this test.
This is where Mock frameworks comes into picture. If we could mock this service and return different results so that we can test our business rules the it would be great and this how the idea of a mocking framework starts.
In my next post I will show you how we will re-factor the existing code to decouple the hard dependency and write the test using the Moq framework…
Note:- Part 2 to follow shortly …
ASP.NET MVC 3 Preview 1 released
I am excited to see that the ASP.NET MVC 3 Preview 1 has been released and you can get it from microsoft download website.
The features I am interested are:-
- Razor View Engine and support for multiple view engines
- Service Location and Dependency Injection Support
And here is this cool video of scott hanselman and Phil haack talking about MVC 3 Preview 1.
Spark View Engine
One of the architecture beauty of ASP.NET MVC is that there are good building block and you can chop and change if you don’t like anything or want to try something different.So i thought I will give it a try and lets see how other view engines fit in is it really that easy to unplug one block and put another one ? Since razor is almost out I thought lets try developing something with an existing view engine other than the WebForms view engine.
I had heard a lot about the spark view engine. The other one which we all have heard is the nHaml view engine.
In order to get started with Spark View engine you have to download it from here.. Just unzip the files into a known folder and we are ready to rock-n-roll.
First we will create an ASP.NET MVC 2 project in visual studio and then add reference to Spark.dll and Spark.Web.Mvc.dll in your project


In order to configure the Spark view engine to be used in your MVC project you can either add configuration setttings it the web.config file or you can do it using the code. I am going to use the code approach to show you how easy it is to add the view engine.

So all you have to do is use the SparkViewFactory class and add it the ViewEngine collection of ASP.NET MVC and things can’t get better than this.
Next we will create a simple View Model object and call it PersonVm.I prefer the Vm convention at the end to indicate it is different than the actual model, its not a good idea to expose your domain model as views.

Now lets create a controller called PersonController and add some code to it’s Index action. Here we are just returning a collection of some PersonVm object.

Now lets create a new view for this controller by creating a folder called Person under views and then add a new text file and name it as Index.spark

Now lets write the code into the view so that the desired HTML can be rendered using the spark view engine. As you can see there a few different things in terms of syntax but overall the concept is the same. I really like the foreach construct and the convenience of the looping code.

Like webforms view engine Spark View engine also has a concept of master page but it is implemented in a slightly different manner.To create a master page you have to create a folder under views and call it Layouts and create a new text file and name it as Application.spark.
With this file you are telling the spark view engine is that this is the master page for all of spark views. I am sure if you look more into it there is a better way of implementing it but for a quick solution this way works.And now we add the following code to the master page.


As you can see when we specify use content=”view” markup in the spark application file the view engine knows this is where the content will be rendered. That’s it compile and hit F5 and viola your page is rendered with the desired results.

So the verdict is yes these blocks are easy to use and can plug and unplug them as per your desire, if you don’t like anything or want to try something different you can do it with out changing millions of line of XML file
Snippet Designer
The snippet designer is a very cool plugin and productivity tool for VS2010. This is very useful for creating custom code snippet/template and the best part is you can export an existing code into custom snippet .I will walk you through step by step on how to maximize it’s use.
Step 1: Download it from codeplex and install.

Step 2: Take your existing code and convert into a snippet.

Step 3: Transform the key code into replacements variables(like macros) by decorating it with “$” on the both ends. In this case I have decorated both the interface IDrive and the method DriveMethod(). ( I know it just should be Drive but I am thinking more from templating point of view)

Step 4: On the properties of the snippet code file set the shorcut word which when typed will generate the code stub.

Step 5: Save the file under Code snippet directory structure.

Step 6:Type in the shortcut,tab it and viola the code is generated.

This could be very useful for your day in day out interface, business layer, service layer kind of code snippet.
Free JQuery ebook
Even though the world cup is over and some of us are feeling sad that it got over so soon, here is a something to cheer about. Yes it’s true a free jquery ebook provided by sitepoint just because Spain won the world cup
. Just enter your email and the link to the book will send to your email and there are some free videos to learn about other technologies too.
The Surprising Truth about what motivates us
We had a team building/bonding exercise where we were given a chance to give presentation of improving communication and motivation. I was surprise that so many highly technical people had creative and yet funny and interesting way to present it.One of the team played this 10 minute video about motivation from Dan Pink . The truth is fascinating and so is the way it is presented, spare 10 minutes and it might change your or your colleague’s life
Group Items Plugin
A friend of mine showed me this plug-in for Visual Studio 2010 which is great for grouping items.

and you can read all about it at Mokosh Blog post.I really like to group various interfaces and its implementing classes together as shown above.