Question

I'm trying to unit test a repository, but what happen is that when I test it I don't get a 100% coverage rather I get a 0% code coverage on that specific method.

I want to test without using third party frameworks, that's why I wanted to use shims and fakes.

Here is the class that I'm trying to test:

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

Here is the test class:

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

Here is the test method:

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

I'm getting 0% code coverage on the GetAll method, how do I get it to 100% and why is it getting 0% ?

Was it helpful?

Solution

Of course you are, your Shim has replaced the functionality completely, thus you're not seeing coverage. Since you're already making sure that context.Authors is overridden, why are you still overriding the GetAll method, which doesn't depend on anything you haven't already overridden?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top