2
단위 테스트를하려고합니다.하지만 테스트 할 때 100 % 적용 범위를 얻지 못하고 특정 방법에 대해 0 % 코드 적용 범위를 얻습니다.단위 테스트 Entity Framework가 shim과 fake를 사용하여 100 % 적용 범위를 반환하지 않습니까?
제 3 자 프레임 워크를 사용하지 않고 테스트하고 싶기 때문에 shim과 fake를 사용하려고했습니다.
[TestMethod]
public void GetAll_MethodIsCalled_RepositoryReturnsCorrectNumberOfAuthorsInTheDbSet()
{
using(ShimsContext.Create())
{
this.SetupRepository();
var authorsCount = this.repository.GetAll().Count();
Assert.AreEqual(authorsCount,3);
}
}
I : 여기
[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());
}
테스트 방법 : 다음
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;
}
}
}
테스트 클래스입니다 : 여기
내가 시험에 노력하고있어 클래스 GetAll 메서드에서 0 % 코드 적용 범위를 얻었는데 어떻게 100 %로 가져오고 왜 0 %를 얻는가?
감사합니다. 지금은 100 % –