Code Generation For C#, WPF, WCF, SQL SERVER/ORACLE/MYSQL and Visual Studio 2013-2019

Tutorial - Using Mocking (with MOQ) in a test application with CodeTrigger
To work with these sample projects you will need CodeTrigger installed, Visual Studio 2013-2017, and a test database with some data.
This tutorial/sample is a follow-on from a previous tutorial/sample that you can find here: Tutorial - Generating a sample repository-pattern test application with CodeTrigger
You need to perform the steps in the previous article before carrying on with the steps in this article.
This guide has been updated for version (released June 10th 2017)

Step 8 - Configure a mocking (moq) test & RUN!

Build the solution you created in the previous articule (steps 1 - 7) and confirm it builds successfully.
Now we create a basic test to demonstrate the usage of mocking (with Moq).
Note that this is not a tutorial on writing Unit tests or Mocking! And our mock object in this sample is fairly basic. The idea of this tutorial/sample is to show CodeTrigger generated code in a code mock scenario.

The following steps are all applied in the SampleRepositoryPattern.Tests project you created in the previous tutorial.

(i) Add a reference to the Moq.dll assembly.

In our testing we used Moq.dll assembly extracted from which you can download from here
We created a folder in the SampleRepositoryPattern.Tests called 'Lib', extracted and copied the Moq.dll assembly into that folder and then added a reference to it.
(ii) Add a reference to the SampleRepositoryPattern.Data project you created in step 2a
(iii) Add a reference to the System.Data assembly
(iv) Create a folder called MockRepository in the project.
(v) Add the following class files to the MockRepository project
The contents of these files are listed at the end of the article.

Thats it, you are ready to consume the business and data layer via your repository pattern.
In the UnitTest1 class, add the following using namespace declarations:

using SampleRepositoryPattern.Tests.MockRepository;
using Moq;

Now all you have to do is prep your business layer objects with the MOCK repository rather than the real one. So you can create a new test in the UnitTest1 class called

which is identical to the test create_update_delete_Category() in the previous article except that the line
ICategoriesRepository iCategoriesRepository = (ICategoriesRepository)repoFactory.CategoriesRepository;
is replaced by the line
ICategoriesRepository iCategoriesRepository = (ICategoriesRepository)new CategoriesRepositoryMock();

And thats it, you have successfully created and unit tested using Mocking (with Moq) a multi-tier, database enabled, repository pattern based application, using CodeTrigger 4.2.
We have mocked out our real database repository where the Category entity is stored, and replaced it with a Mock CategoryRepository
which persists category objects into a list, and returns mock category objects when asked.
So you can run the unit test and 'create' / 'save' / 'update' and 'delete' category objects as you normally would, but without hitting the backend database.

using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Configuration;
using SampleRepositoryPattern.Data.Interfaces;

namespace SampleRepositoryPattern.Tests.MockRepository
	public partial class ConnectionProviderMock : IDisposable, IzSampleRepositoryPatternConn_TxConnectionProvider
            public ConnectionProviderMock(){ Init(); }
	    protected virtual void Init(){ 	}
	    public void Dispose(){	}
	    protected virtual void Dispose(bool isDisposing){	}

	    public virtual void OpenConnection(){	}
	    public virtual void CloseConnection(bool commit){	}
	    public virtual void BeginTransaction(string trans){	}
	    public virtual void CommitTransaction(){	}
	    public virtual void RollbackTransaction(string trans){	}

	    public virtual SqlTransaction CurrentTransaction
	    { get{ return null; }}

	    public virtual SqlConnection Connection
	    { get{ return null;	}}

        public virtual Int32 TransactionCount { get; set; }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SampleRepositoryPattern.Data;
using SampleRepositoryPattern.Data.Interfaces;
using SampleRepositoryPattern.Business.Interfaces;
using SampleRepositoryPattern.Business.Repository.Interfaces;
using Moq;

namespace SampleRepositoryPattern.Tests.MockRepository
    public class CategoriesRepositoryMock : ICategoriesRepository
        private List<IDAOCategories> _listCategories = null;
        IzSampleRepositoryPatternConn_TxConnectionProvider _connectionProvider = new ConnectionProviderMock();

        public CategoriesRepositoryMock()
            _listCategories = CreateList();

        public virtual IzSampleRepositoryPatternConn_BaseData BaseData(IDAOCategories iDaoCategories)
            var mockBaseData = new Mock<IzSampleRepositoryPatternConn_BaseData>();
            return mockBaseData.Object;

        public virtual IzSampleRepositoryPatternConn_TxConnectionProvider ConnectionProvider
            { return _connectionProvider; }
            { _connectionProvider = value; }

        public virtual IDAOCategories New()
            var mockCategory = new Mock<IDAOCategories>();
            mockCategory.SetupProperty(x => x.CategoryID, -1);
            mockCategory.SetupProperty(x => x.CategoryName, "My mock category");
            mockCategory.SetupProperty(x => x.Description, "Description of my mock category");
            mockCategory.SetupProperty(x => x.CtrVersion, 0);
            return mockCategory.Object;

        public virtual void Update(IDAOCategories x)
            IDAOCategories theX = SelectOne(x.CategoryID);
            if (theX != null)
                theX.CategoryName = x.CategoryName;
                theX.Description = x.Description;
                theX.CtrVersion = theX.CtrVersion + 1;

        public virtual void Insert(IDAOCategories x)
            x.CategoryID = _listCategories.Count;

        public virtual void Delete(IDAOCategories x)
            int id = (int)x.CategoryID;
            IDAOCategories cat = SelectOne(id);

        public virtual IDAOCategories SelectOne(Int32? categoryID)
            foreach (IDAOCategories cat in _listCategories)
                if (cat.CategoryID == categoryID)
                    return cat;
            return null;

        private List<IDAOCategories> CreateList()
            _listCategories = new List<IDAOCategories>();
            for (int k = 0; k < 10; k++)

            return _listCategories;

        public virtual IList<IDAOCategories> SelectAll()
            return _listCategories;

        public virtual Int32 SelectAllCount()
            return _listCategories.Count;

        public virtual IDictionary<string, IList<object>> SelectAllByCriteriaProjection(IList<IDataProjection> listProjection, 
                IList<IDataCriterion> listCriterion, IList<IDataOrderBy> listOrder, IDataSkip dataSkip, IDataTake dataTake)
        { throw new NotImplementedException(); }

        public virtual IList<IDAOCategories> SelectAllByCriteria(IList<IDataCriterion> listCriterion, IList<IDataOrderBy> listOrder, 
                IDataSkip dataSkip, IDataTake dataTake)
        { throw new NotImplementedException(); }

        public virtual Int32 SelectAllByCriteriaCount(IList<IDataCriterion> listCriterion)
        { throw new NotImplementedException(); }

        Int32 _transactionCount = 0;
        public virtual Int32 TransactionCount
            get { return _transactionCount; }
            set { _transactionCount = value; }