Click here to Skip to main content
12,294,775 members (66,387 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


18 bookmarked

An Introduction to MOQ

, 20 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.


This post introduces how we can mock our dependencies and carry on with an uninterrupted unit testing using MOQ.


Many a times, we see that we have to depend on many external dependencies while development or unit testing our code for example, depending on a group of web service or WCF services providing us some data, stored procedure for some magic values or a Flat/XML file system.

In such a case, it's very difficult to code or unit test your application without hard coding the dependencies reason being those dependencies might not been in place or developed yet or we have been provided only with contracts with no concrete implementation.

In this post, I have decided to take up a simple example of a banking transaction module consisting of entities like Customer class, TransactionManager in business logic layer.

Transaction manager depends on a web service call for validating Social security number for the customer and a data base call for identifying the authentication of the customer.

Using the Code

1. We have the following web service contract:

using System;
public interface IValidateSSN
    bool Validate(string SSN);

2. We have the following Data access layer contract:

public interface IValidateCustomer
    bool Validate(string CustomerID);

3. We have the following Customer class:

public class Customer
    public string SSN{get;set;}
    public string FirstName{get;set;} 
    public string LastName{get;set;} 

4. We have transaction helper class:

public class  TransactionHelper
    public bool DoTransaction(Customer cust)
        return true;

5. We have TransactionManager class:

public class TransactionManager
    public bool ValidateCustomer(Customer customer)
        //need to validate customer SSN

        // IValidateCustomer.Validate(Custom.SSN);
        // but no concrete impementation at place 

        // need to validate Customer authentication but no concrete implementation 

        TransactionHelper helper = new TransactionHelper();
        bool res=helper.DoTransaction((customer));
        return res;

Completing the unit testing of TransactionManager's validateCustomer method using MOQ .

Download MOQ from

Create a test project in VS2010 and refer MOQ.dll and your assembly in the test class file:

using MOQ.dll;
using System.Linq;
public class TransactionManagerTest
    public void ValidateCustomerTest()
        TransactionManager target = new TransactionManager
        Customer customer = new Customer();  
        bool expected = false;
        bool actual;
        customer.CustomrID = "33333";
        var IValidateCustomer = new Mock<ivalidatecustomer>();
        var ivalidatessn = new Mock<ivalidatessn>();
        ivalidatessn.Setup((m => m.Validate(customer.SSN))).Returns(true);
        bool res=target.ValidateCustomer(customer);


Here MOQ is responsible for creating a proxy/fake for us and we have used setup method to set the expected results of the fake object.In order to Mock a property/method of a class, we need to mark it with virtual keyword.
Interfaces are mockable by default. It comes very handy when you have contracts with you but no concrete implementation or your implementation/behavior is volatile.


We avoided the two dependencies with the help of MOQ, which according to the contract/interface created a mock object for us and helped us to carry on with our unit testing.


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


About the Author

India India
Computer geek very much interested in learning and adopting latest and cutting edge technologies .

Expertise in,c#,WCF,web services,Jquery,MVC ,TTD's and design principles and patterns applying SOLID

Designation - Architect
Group type: Organisation

9 members

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Member 116656615-May-15 1:28
memberMember 116656615-May-15 1:28 
GeneralMy vote of 1 Pin
Member 116656615-May-15 1:26
memberMember 116656615-May-15 1:26 
GeneralRe: My vote of 1 Pin
imsauravroy5-May-15 1:34
groupimsauravroy5-May-15 1:34 
GeneralRe: My vote of 1 Pin
Member 116656615-May-15 2:34
memberMember 116656615-May-15 2:34 
Questionsome improvements - for beginners Pin
monishankar5-Mar-15 22:22
membermonishankar5-Mar-15 22:22 
QuestionUse a period (.) to separate sentences Pin
C-codist15-Sep-14 13:25
memberC-codist15-Sep-14 13:25 
QuestionMoq Introduction Pin
Pascualito26-Apr-14 10:52
memberPascualito26-Apr-14 10:52 
GeneralMy vote of 2 Pin
J. Ryan Jones29-Mar-14 5:03
memberJ. Ryan Jones29-Mar-14 5:03 
GeneralMy vote of 2 Pin
Sprint897-Mar-14 0:41
professionalSprint897-Mar-14 0:41 
GeneralA nice tip, but this topic is worthy of a full blown article... Pin
CIDev19-Apr-11 7:08
memberCIDev19-Apr-11 7:08 
GeneralSuggestion Pin
Andrew Rissing17-Apr-11 12:42
memberAndrew Rissing17-Apr-11 12:42 
GeneralRe: Suggestion Pin
Riz Thon18-Apr-11 15:37
memberRiz Thon18-Apr-11 15:37 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 20 Apr 2011
Article Copyright 2011 by imsauravroy
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid