C# 解耦EntityFramework進行單元測試

來源:互聯網
上載者:User
1. 首先EF的Repository需要抽象的行為提到介面中。

例如 :

public interface IXXXContext : IDisposable    {        IXXXContext NewInstance();// db sets        DbSet<AAABBB> aaa { get; set; }...// common Database Database { get; }        DbContextConfiguration Configuration { get; }        int SaveChanges();        Task<int> SaveChangesAsync();// store pros...        IStorePro1 StorePro1 { get; }...}


然後就可以使用DataContext和TestDataContext實現這個介面。其中TestDataContext是在UT中使用的,DataContext是自動產生的。

TestDataContext還需要以下幾個類進行類比。

 public class TestDbSet<TEntity> : DbSet<TEntity>, IQueryable, IEnumerable<TEntity>, IDbAsyncEnumerable<TEntity>         where TEntity : class    {        ObservableCollection<TEntity> _data;        IQueryable _query;        public TestDbSet()        {            _data = new ObservableCollection<TEntity>();            _query = _data.AsQueryable();        }        public override TEntity Add(TEntity item)        {            _data.Add(item);            return item;        }        public override TEntity Remove(TEntity item)        {            _data.Remove(item);            return item;        }        public override TEntity Attach(TEntity item)        {            _data.Add(item);            return item;        }        public override TEntity Create()        {            return Activator.CreateInstance<TEntity>();        }        public override TDerivedEntity Create<TDerivedEntity>()        {            return Activator.CreateInstance<TDerivedEntity>();        }        public override ObservableCollection<TEntity> Local        {            get { return _data; }        }        Type IQueryable.ElementType        {            get { return _query.ElementType; }        }        Expression IQueryable.Expression        {            get { return _query.Expression; }        }        IQueryProvider IQueryable.Provider        {            get { return new TestDbAsyncQueryProvider<TEntity>(_query.Provider); }        }        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()        {            return _data.GetEnumerator();        }        IEnumerator<TEntity> IEnumerable<TEntity>.GetEnumerator()        {            return _data.GetEnumerator();        }        IDbAsyncEnumerator<TEntity> IDbAsyncEnumerable<TEntity>.GetAsyncEnumerator()        {            return new TestDbAsyncEnumerator<TEntity>(_data.GetEnumerator());        }    }    internal class TestDbAsyncQueryProvider<TEntity> : IDbAsyncQueryProvider    {        private readonly IQueryProvider _inner;        internal TestDbAsyncQueryProvider(IQueryProvider inner)        {            _inner = inner;        }        public IQueryable CreateQuery(Expression expression)        {            return new TestDbAsyncEnumerable<TEntity>(expression);        }        public IQueryable<TElement> CreateQuery<TElement>(Expression expression)        {            return new TestDbAsyncEnumerable<TElement>(expression);        }        public object Execute(Expression expression)        {            return _inner.Execute(expression);        }        public TResult Execute<TResult>(Expression expression)        {            return _inner.Execute<TResult>(expression);        }        public Task<object> ExecuteAsync(Expression expression, CancellationToken cancellationToken)        {            return Task.FromResult(Execute(expression));        }        public Task<TResult> ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken)        {            return Task.FromResult(Execute<TResult>(expression));        }    }    internal class TestDbAsyncEnumerable<T> : EnumerableQuery<T>, IDbAsyncEnumerable<T>, IQueryable<T>    {        public TestDbAsyncEnumerable(IEnumerable<T> enumerable)            : base(enumerable)        { }        public TestDbAsyncEnumerable(Expression expression)            : base(expression)        { }        public IDbAsyncEnumerator<T> GetAsyncEnumerator()        {            return new TestDbAsyncEnumerator<T>(this.AsEnumerable().GetEnumerator());        }        IDbAsyncEnumerator IDbAsyncEnumerable.GetAsyncEnumerator()        {            return GetAsyncEnumerator();        }        IQueryProvider IQueryable.Provider        {            get { return new TestDbAsyncQueryProvider<T>(this); }        }    }    internal class TestDbAsyncEnumerator<T> : IDbAsyncEnumerator<T>    {        private readonly IEnumerator<T> _inner;        public TestDbAsyncEnumerator(IEnumerator<T> inner)        {            _inner = inner;        }        public void Dispose()        {            _inner.Dispose();        }        public Task<bool> MoveNextAsync(CancellationToken cancellationToken)        {            return Task.FromResult(_inner.MoveNext());        }        public T Current        {            get { return _inner.Current; }        }        object IDbAsyncEnumerator.Current        {            get { return Current; }        }    }


使用樣本:

[TestMethod]        public void TestMethod1()        {            var mockSet = new Mock<DbSet<BLACKLISTED_TICKET>>();            var mockContext = new Mock<TicketDataContextTest>();            mockContext.Setup(m => m.BLACKLISTED_TICKET).Returns(new TestDbSet<BLACKLISTED_TICKET>());            var context = mockContext.Object;            context.BLACKLISTED_TICKET.Add(new BLACKLISTED_TICKET()            {                TicketNumber = "aaa",                CreatedDateTime = DateTime.Now,                Id = 1,                ModifiedDateTime = DateTime.Now,                STATUS = "1"            });            Assert.IsTrue(context.BLACKLISTED_TICKET.First().Id == 1);        }

如果使用了預存程序,需要額外定義預存程序的介面。
例如:

IStorePro {...}StorePro : IStorePro{...}StoreProFake: IStorePro{}

然後IDataContext負責返回預存程序的執行個體

IDataContext{...IStorePro GetStorePro();...}

以上就是C# 解耦EntityFramework進行單元測試的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.