c# - 使用 shim 和 fakes 单元测试 Entity Framework 不会返回 100% 覆盖率?

标签 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%?

最佳答案

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

关于c# - 使用 shim 和 fakes 单元测试 Entity Framework 不会返回 100% 覆盖率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16874120/

相关文章:

visual-studio - 代码覆盖率结果定期给出: Empty results generated

c# - MSTest 找不到程序集

c# - SQL Server 中串联的最佳实践?

c# - 在 C# 中使用 @keyword——坏主意?

ios - Cocoapods 库在单元测试期间未正确返回 isKindOfClass

objective-c - 我如何在 OCMock 中模拟接受句柄作为参数的方法?

javascript - 如果它们是只读的,我如何模拟 ES6 导入的模块?

c# - 自定义消息和默认消息

c# - System.Windows.Forms.AxHost.InvalidActiveXStateException 未处理

c# - 如何将sql多个LEFT JOIN转换为linq