2

我正在尝试对存储库进行单元测试,但是发生的情况是,当我对其进行测试时,我没有获得 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);
        }
    }

我在 GetAll 方法上得到 0% 的代码覆盖率,我如何让它达到 100% 以及为什么它得到 0% ?

4

1 回答 1

2

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

于 2013-06-01T18:21:37.977 回答