2

我想單元測試一個存儲庫,但是當我測試它時發生了什麼,我沒有得到100%的覆蓋率,而是我在該具體方法上獲得了0%的代碼覆蓋率。單元測試實體框架使用墊片和假貨不會返回100%覆蓋率?

我想測試,而不使用第三方框架,這就是爲什麼我想使用墊片和假貨。

這裏是我想測試類:

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

回答

2

當然你是,你的Shim已經完全取代了功能,因此你沒有看到覆蓋。既然你已經確定context.Authors被覆蓋,爲什麼你仍然覆蓋GetAll方法,它不依賴於你尚未被覆蓋的任何東西?

+0

謝謝,現在我得到100% –