我正在为数据库调用方法使用 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);
}