Click here to Skip to main content
15,890,741 members
Articles / Programming Languages / C#

Simplify Automation Unit Testing Using IOC And MOQ

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
24 Dec 2016CPOL3 min read 13.3K   7   3
--

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)


Written By
Software Developer
India India
I am a Full stack developer in Microsoft technologies, Android development and MEAN stack, I have played a role like an individual contributor / Tech lead / Architect.

I like to do experiments with new approach and various combinations of technologies.

Comments and Discussions

 
BugImages are not displaying. Pin
Abhith Rajan19-Dec-16 7:54
Abhith Rajan19-Dec-16 7:54 
QuestionIntegration testing Pin
John Brett15-Dec-16 3:02
John Brett15-Dec-16 3:02 
My only issue with this approach (aside from the images not working and no download via CodeProject) is the name.

What you're describing, if you're testing a hierarchy of classes together, is integration testing.
It has its place, but unit testing is very specifically about testing individual units of code.

Unit testing should be the first level of testing, providing fine-grained control over all aspects of the class's dependencies.
With your approach to integration testing, there's a tendency to set up one scenario (a pre-populated in-memory database) and run multiple tests against it, which can result in some test cases being overlooked, or tests being bent into what's available in the test database rather than creating a new test database specifically for each test.

Integration testing is itself useful for ensuring that all of the components cooperate together, but this should come in after the unit tests pass.

John
AnswerRe: Integration testing Pin
Makrand Vichare18-Dec-16 0:52
Makrand Vichare18-Dec-16 0:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.