如何在moq中调试验证?

时间:2012-10-31 22:03:14

标签: c# .net unit-testing moq autofixture

我正在使用IDoco类附带的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); 

}

2 个答案:

答案 0 :(得分:2)

SUT中的代码使用IUnitOfWorkProvider生成IUnitOfWork:

using (var unitOfWork = unitOfWorkProvider.GetUnitOfWork())
{
    MyDomainObject myDomain = anotherService.getMyDomain(DateTime.Now, 100);
    unitOfWork.Db.Insert(myDomain); 
}

您当前尝试模拟的IUnitOfWork实例是其他一些实例。它们不是由IUnitOfWorkProvider生成的。

假设将IUnitOfWorkProvider注入到您的SUT中,您应该可以将其冻结并从那里开始。这样的事情应该有效:

var fixture = new Fixture().Customize(new AutoMoqCustomization());
var uowProviderStub = fixture.Freeze<Mock<IUnitOfWorkProvider>>();
var uowMock = fixture.CreateAnonymous<Mock<IUnitOfWork>>();
var sut = fixture.CreateAnonymous<MyService>();

uowProviderStub.Setup(p => p.GetUnitOfWork()).Returns(uowMock.Object);
uowMock
    .Setup(x => x.Db.Insert(It.IsAny<MyDomainObject>()))
    .Verifiable();

// etc.

这有点麻烦,这真的是试图告诉你Law of Demeter violation不是最好的设计的测试......

答案 1 :(得分:0)

你需要让你的模拟电话可以验证

unitOfWork
    .Setup(x => x.Db.Insert(It.IsAny<MyDomainObject>()))
    .Verifiable();