Home > dotnet 4, SOLID > The Art of Mocking – Part 1

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.

Dependecies diagram

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.

CreditHistoryCheck

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 )

LoanApprovalService

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)

Unit Test

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 …

Advertisements
Categories: dotnet 4, SOLID Tags: , , ,
  1. Yogesh
    July 30, 2010 at 5:54 am

    Good start buddy. Can’t wait to read the next part in the series.

  2. Gorang
    August 6, 2010 at 2:48 am

    A very good article, this highlights issues face while unit test a component which has external dependencies. Another example of could be a dependencies on persistence layer. Introducing a mock for this improves unit testing a lot.

    I look forward to part 2

  3. Donte
    May 30, 2014 at 12:52 am

    Excellent blog you’ve got here.. It’s difficult to find excellent writing like yours nowadays.
    I honestly appreciate people like you! Take care!!

  1. August 2, 2010 at 2:45 am
  2. August 10, 2010 at 1:43 am
  3. August 16, 2010 at 1:40 pm
  4. August 18, 2010 at 5:39 am
  5. December 9, 2010 at 12:24 am

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: