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.
|
//ConnectionProviderMock.cs
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; }
}
}
//CategoriesRepositoryMock.cs
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
{
get
{ return _connectionProvider; }
set
{ _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);
mockCategory.As<IzSampleRepositoryPatternConn_BaseData>();
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;
_listCategories.Add(x);
}
public virtual void Delete(IDAOCategories x)
{
int id = (int)x.CategoryID;
IDAOCategories cat = SelectOne(id);
_listCategories.Remove(cat);
}
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++)
Insert(New());
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; }
}
}
}
|