Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Simplify Automation Unit Testing Using IOC And MOQ

5.00/5 (1 vote)
24 Dec 2016CPOL3 min read 13.8K  
--

Introduction

Few years ago, I got an opportunity to work on a project which needed a migration from web form to MVC and I was assigned a task to come up with a good testing approach for testing the come site. Afterwards I worked with various companies but I find a such approach and I helped them to simplify the unit testing using the same approach. It may be helpful to you also I would like to know what do you think about it.

*** this approach is not an integration testing but to minimize the repeatation of work so that unit test cases can be maintained with less efforts.(most of the time updating test cases  or writing test cases for classes of same pattern (only insert.update,delete, view and not much complex logic) is very boring and repeatative.

The example code is in very simple form and you can improvise it as per your need. it is available at  https://github.com/vicharemakrand/SampleCode-Testing

Background

You should have a knowledge of 

  • MOQ
  • IOC – e.g. Structure map is used in the example
  • Unit Testing Framework – e.g. MS unit / Nunit / Xunit
  • Nbuilder like similar for generating mock data.

Take Away

  • Using IOC to create mock objects
  • Maintain test cases with less efforts
  • Helps to maintain uniformity in the code which is very useful in scrum based projects.

Drawbacks of Traditional approach

  • Lots of repeated code where we mock the dependencies
  • Need quite efforts to keep the test cases update to date.
  • It may lead to lose focus on testing business cases properly.

About my approach

Instead of mocking each method for each test and deciding the output, we will be using in- memory database which will be generated using Nbuilder and the dependencies will be handled by an IOC.

Also, we don’t mock service layer method using the MOQ but we use all the methods as it is to call repository layer which is mocked to use in memory database.

 Let’s start from Bottom Up. The sample code covers User table which is available on GitHub at https://github.com/vicharemakrand/SampleCode-Testing

 

Sample Code Explanation

1. In-memory mock database

We need a mock data for each entity model which is mapped to a database table.

  • Create some mock records for user table

  • Create a class to store the mock records in a collection

2. Create a mock repository object using MOQ

Here we setup repository methods but instead of hardcoding result. We do actual simple operation related to the methods. This approach same lots of duplicate code and maintenance efforts.

3. IOC – interface and class mapping

Here we map interfaces with generated mock objects which also store mockdata with them.

4. Create IOC container for test project.

5. Now writing test cases will be quite simpler

5.1 Test cases for Service layer

5.2 Test cases for Repository layer

5.3 MVC controller Test cases

 

Final Thought

In the real project this approach can be implemented various way. you dont have to implement it completely. if dont agree with this aproach 100% then also you can pick up few things like using IOC for creation of mock object or repository layer test cases.

Let me know what do you think and suggest about this application architecture.

I am going to write a article which will demostrate writing test cases for old projects where method names are not uniform.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)