单元测试实体框架没有使用shims和fake返回100%的覆盖率

本文关键字:fake 返回 100% 覆盖率 shims 框架 实体 单元测试 | 更新日期: 2023-09-27 18:16:37

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

我想在不使用第三方框架的情况下进行测试,这就是为什么我想使用shims和fakes。

这是我要测试的类:

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

我在gettall方法上获得0%的代码覆盖率,我如何使它达到100%,为什么它获得0% ?

单元测试实体框架没有使用shims和fake返回100%的覆盖率

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