データベース呼び出しメソッドにIDatabaseクラスに付属しているNPocoを使用しています。NPoco Insertメソッドに入るオブジェクトに正しいデータ(ドメインオブジェクトの形式)があることを確認したいと思います。
public interface IUnitOfWorkProvider
{
IUnitOfWork GetUnitOfWork();
}
public interface IUnitOfWork : IDisposable
{
void Commit();
IDatabase Db { get; }
void SetOneTimeCommandTimeout(int timeout);
void SetGlobalCommandTimeout(int timeout);
}
public interface IDatabase : IDatabaseQuery
{
IDbConnection Connection { get; }
IDbTransaction Transaction { get; }
void AbortTransaction();
void BeginTransaction();
void BeginTransaction(IsolationLevel? isolationLevel);
void CompleteTransaction();
IDataParameter CreateParameter();
int Delete(object poco);
int Delete<T>(object pocoOrPrimaryKey);
int Delete<T>(Sql sql);
int Delete<T>(string sql, params object[] args);
int Delete(string tableName, string primaryKeyName, object poco);
int Delete(string tableName, string primaryKeyName, object poco, object primaryKeyValue);
void Dispose();
Transaction GetTransaction();
Transaction GetTransaction(IsolationLevel? isolationLevel);
object Insert(object poco);
object Insert(string tableName, string primaryKeyName, object poco);
object Insert(string tableName, string primaryKeyName, bool autoIncrement, object poco);
void Save(object poco);
void Save(string tableName, string primaryKeyName, object poco);
IDatabase SetTransaction(IDbTransaction tran);
int Update(object poco);
int Update<T>(Sql sql);
int Update(object poco, IEnumerable<string> columns);
int Update(object poco, object primaryKeyValue);
int Update<T>(string sql, params object[] args);
int Update(object poco, object primaryKeyValue, IEnumerable<string> columns);
int Update(string tableName, string primaryKeyName, object poco);
int Update(string tableName, string primaryKeyName, object poco, IEnumerable<string> columns);
int Update(string tableName, string primaryKeyName, object poco, object primaryKeyValue);
int Update(string tableName, string primaryKeyName, object poco, object primaryKeyValue, IEnumerable<string> columns);
}
// my test class file
private IFixture fixture;
private Mock<IUnitOfWork> unitOfWork;
private MyService myService;
private Mock<IDatabase> database; // new based on responses
[SetUp]
public void Setup()
{
fixture = new Fixture().Customize(new AutoMoqCustomization());
database = fixture.Freeze<Mock<IDatabase>>();// new based on responses
unitOfWork = fixture.Freeze<Mock<IUnitOfWork>>();
myService = fixture.CreateAnonymous<MyService>();
}
[Test]
public MyTest()
{
// fails
unitOfWork.Setup(x => x.Db.Insert(It.IsAny<MyDomainObject>()));
myService.CallMyMethod();
unitOfWork.Verify(x => x.Db.Insert(It.IsAny<MyDomainObject>()));
// fails
unitOfWork.Setup(x => x.Db.Insert(It.IsAny<object>()));
myService.CallMyMethod();
unitOfWork.Verify(x => x.Db.Insert(It.IsAny<object>()));
// fails (this was a try based on responses)
database.Setup(x => x.Insert(It.IsAny<object>()));
myService.CallMyMethod();
database.Verify(x => x.Insert(It.IsAny<object>()));
// passes
unitOfWork.Setup(x => x.Db.Insert(It.IsAny<object>()));
myService.CallMyMethod();
unitOfWork.Verify();
}
public class MyDomainObject
{
public void Id {get; set;}
}
呼び出されているコード(検証をトリガーするもの)
using (var unitOfWork = unitOfWorkProvider.GetUnitOfWork())
{
MyDomainObject myDomain = anotherService.getMyDomain(DateTime.Now, 100);
unitOfWork.Db.Insert(myDomain);
}