Esempio n. 1
0
        // ---------------------------------------------------------------------------------------------

        /// <summary>
        /// Mocks a context and sets a list of objects to be returned by the repository
        /// </summary>
        /// <typeparam name="TContext">The context of the module</typeparam>
        /// <typeparam name="TModel">The model of the repository</typeparam>
        /// <param name="container">The container</param>
        /// <param name="mockedCollection">The collection that should be returned from the mocked repository</param>
        protected void MockContext <TContext, TModel>(TinyIoCContainer container, ICollection <TModel> mockedCollection)
            where TContext : class, IDbContext
            where TModel : class
        {
            var mockedContext = container.InstantiateAndInjectMock <TContext>();

            var queryable = mockedCollection.AsQueryable();

            var dbSet = new Mock <DbSet <TModel> >();

            dbSet.As <IQueryable <TModel> >().Setup(m => m.Provider).Returns(queryable.Provider);
            dbSet.As <IQueryable <TModel> >().Setup(m => m.Expression).Returns(queryable.Expression);
            dbSet.As <IQueryable <TModel> >().Setup(m => m.ElementType).Returns(queryable.ElementType);
            dbSet.As <IQueryable <TModel> >().Setup(m => m.GetEnumerator()).Returns(queryable.GetEnumerator);
            dbSet.Setup(s => s.Add(It.IsAny <TModel>())).Callback <TModel>(mockedCollection.Add);
            dbSet.Setup(s => s.Include(It.IsAny <string>())).Returns(dbSet.Object);
            dbSet.Setup(x => x.AsNoTracking()).Returns(dbSet.Object);

            mockedContext.Setup(x => x.Set <TModel>()).Returns(dbSet.Object);
        }