单元测试实体框架不使用垫片和假货返回100%的覆盖率?

时间:2013-06-01 15:12:05

标签: c# unit-testing vs-unit-testing-framework microsoft-fakes

我正在尝试对存储库进行单元测试,但是当我测试它时,我没有获得100%的覆盖率,而是在该特定方法上得到0%的代码覆盖率。

我想在不使用第三方框架的情况下进行测试,这就是我想使用垫片和假货的原因。

以下是我要测试的课程:

namespace AbstractFactory.Repository
{
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;

    /// <summary>
    /// This class serves as the structure of the Author repository using a database
    /// </summary>
    public class DbAuthorRepository : IRepository<Author>
    {
        /// <summary>
        /// The context
        /// </summary>
        private AbstractFactoryPatternEntities context;

        /// <summary>
        /// Initializes a new instance of the <see cref="DbAuthorRepository"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public DbAuthorRepository(AbstractFactoryPatternEntities context)
        {
            this.context = context;
        }

        /// <summary>
        /// Adds the specified author.
        /// </summary>
        /// <param name="author">The author.</param>
        public void Add(Author author)
        {
            this.context.Authors.Add(author);
        }

        /// <summary>
        /// Removes the specified author.
        /// </summary>
        /// <param name="author">The author.</param>
        public void Remove(Author author)
        {
            this.context.Authors.Remove(author);
        }

        /// <summary>
        /// Saves this instance.
        /// </summary>
        public void Save()
        {
            this.context.SaveChanges();
        }

        /// <summary>
        /// Gets all.
        /// </summary>
        /// <returns>
        /// returns a list of all the authors
        /// </returns>
        public IEnumerable<Author> GetAll()
        {
            List<Author> result = this.context.Authors.ToList();

            return result;
        }

        /// <summary>
        /// Gets the by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns>
        /// returns an entity
        /// </returns>
        public Author GetById(int id)
        {
            Author result = this.context.Authors.Find(id);
            ////this.context.Authors.Single(a => a.AuthorId == id);

            return result;
        }
    }
}

这是测试类:

[TestClass]
    public class DbAuthorRepositoryTest
    {
        private ShimAbstractFactoryPatternEntities shimContext;

        private ShimDbSet<Author> shimAuthors;

        private List<Author> listAuthors;

        private DbAuthorRepository repository;

        private void SetupShims()
        {
            this.shimContext = new ShimAbstractFactoryPatternEntities(new AbstractFactoryPatternEntities());

            this.listAuthors = new List<Author>
            {
                new Author { AuthorId = 2, FirstName = "Test2", LastName = "Test2" },
                new Author { AuthorId = 3, FirstName = "Test3", LastName = "Test3" },
                new Author { AuthorId = 4, FirstName = "Test4", LastName = "Test4" }
            };

            this.shimAuthors = new ShimDbSet<Author>();

            this.shimAuthors.Bind(this.listAuthors.AsQueryable());

            ShimAbstractFactoryPatternEntities.AllInstances.AuthorsGet = (a) => { return shimAuthors; };

            ShimDbAuthorRepository.AllInstances.GetAll = (a) => { return this.shimContext.Instance.Authors.ToList(); };

            //return this.shimContext.Instance.Authors.ToList();

            //return this.shimAuthors.Instance.ToList() as IEnumerable<Author>;

            //ShimDbSet<Author>.AllInstances.FindObjectArray = (a, b) => { a.ToList(); return shimAuthors.Instance.Contains(b) ; };
        }

        private void SetupRepository()
        {
            this.SetupShims();

            this.repository = new DbAuthorRepository(new AbstractFactoryPatternEntities());
        }

以下是测试方法:

[TestMethod]
    public void GetAll_MethodIsCalled_RepositoryReturnsCorrectNumberOfAuthorsInTheDbSet()
    {
        using(ShimsContext.Create())
        {
            this.SetupRepository();
            var authorsCount = this.repository.GetAll().Count();
            Assert.AreEqual(authorsCount,3);
        }
    }

我在GetAll方法上获得0%的代码覆盖率,如何将其提升至100%以及为何获得0%?

1 个答案:

答案 0 :(得分:2)

当然你是,你的Shim完全取代了这个功能,因此你没有看到报道。由于您已经确定已覆盖context.Authors,为什么还要覆盖GetAll方法,这种方法不依赖于您尚未覆盖的任何内容?