简体   繁体   English

阻止孩子们在Moq / AutoFixture中嘲笑

[英]Stop Children Mocking in Moq/AutoFixture

I am having a problem where either moq or Ploeh.AutoFixture.AutoMoq is mocking all the children interfaces when in one case I want one of them to be null. 我有一个问题,其中moq或Ploeh.AutoFixture.AutoMoq正在模拟所有子接口,在一种情况下我希望其中一个为null。

I am using Npoc and it has an Interface called IDatabase 我使用的是Npoc,它有一个名为IDatabase的接口

 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);
    }

I don't want IDbTransaction Transaction { get; } 我不想要IDbTransaction Transaction { get; } IDbTransaction Transaction { get; } to be mocked as I want it to be null but it is getting mocked. IDbTransaction Transaction { get; }被嘲笑,因为我希望它是空,但它是越来越嘲笑。

I then also have a IUnitOfWork that has the IDatabase interface in it. 然后我还有一个具有IDatabase接口的IUnitOfWork。

  public interface IUnitOfWork : IDisposable 
    {
        void Commit();
        IDatabase Db { get; }
        void SetOneTimeCommandTimeout(int timeout);
        void SetGlobalCommandTimeout(int timeout);
        void BeginTransaction();

    }

// code //代码

 fixture = new Fixture().Customize(new AutoMoqCustomization());
 fixture.Freeze<Mock<IDatabase>>();
 var test = fixture.CreateAnonymous<MyService>();

Since Transaction is a readonly property I don't know how to set it back to null. 由于Transaction是一个只读属性,我不知道如何将其设置为null。

Try 尝试

  fixture = new Fixture().Customize(new AutoMoqCustomization());
  var mock = fixture.Freeze<Mock<IDatabase>>();
  mock.SetupGet(o => o.Transaction).Returns((IDbTransaction)null);
  var test = fixture.CreateAnonymous<MyService>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM