单元测试通用存储库

时间:2014-03-11 10:19:36

标签: entity-framework unit-testing nunit entity-framework-6 fakeiteasy

我对单元测试很新,我遇到了一些问题,在我的应用程序中对通用存储库进行单元测试。我在ASP.NET MVC应用程序中实现了工作单元模式。我的课程看起来像这样:

public class UnitOfWork : IUnitOfWork
{
    private bool disposed = false;

    private IGenericRepository<Shop> _shopRespository;

    public UnitOfWork(PosContext context)
    {
        this.Context = context;
    }

    public PosContext Context { get; private set; }

    public IGenericRepository<Shop> ShopRepository
    {
        get
        {
            return this._shopRespository ?? (this._shopRespository = new GenericRepository<Shop>(this.Context));
        }
    }

    public void SaveChanges()
    {
        this.Context.SaveChanges();
    }

    public void Dispose()
    {
        this.Dispose(true);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                this.Context.Dispose();
            }
            this.disposed = true;
        }
    }
}

public class PosContext : DbContext, IPosContext
{
    public DbSet<Shop> Shops { get; private set; }
}

public class GenericRepository<T> : IGenericRepository<T>
    where T : class
{
    private readonly PosContext context;
    private readonly DbSet<T> dbSet;

    public GenericRepository(PosContext context)
    {
        this.context = context;
        this.dbSet = context.Set<T>();
    }

    public virtual IEnumerable<T> Get(
        Expression<Func<T, bool>> filter = null,
        Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
        string includeProperties = "")
    {
        IQueryable<T> query = this.dbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            return orderBy(query).ToList();
        }
        else
        {
            return query.ToList();
        }
    }

    public virtual T Find(object id)
    {
        return this.dbSet.Find(id);
    }

    public virtual void Add(T entity)
    {
        this.dbSet.Add(entity);
    }

    public virtual void Remove(object id)
    {
        T entityToDelete = this.dbSet.Find(id);
        this.Remove(entityToDelete);
    }

    public virtual void Remove(T entityToDelete)
    {
        if (this.context.Entry(entityToDelete).State == EntityState.Detached)
        {
            this.dbSet.Attach(entityToDelete);
        }
        this.dbSet.Remove(entityToDelete);
    }

    public virtual void Update(T entityToUpdate)
    {
        this.dbSet.Attach(entityToUpdate);
        this.context.Entry(entityToUpdate).State = EntityState.Modified;
    }

我正在使用NUnit和FakeItEasy来编写我的单元测试。在我的设置函数中,我创建了一个带有假PosContext对象的UnitIfWork对象。然后我用几个Shop对象填充上下文。

[SetUp]
public void SetUp()
{
    this.unitOfWork = new UnitOfWork(A.Fake<PosContext>());
    this.unitOfWork.ShopRepository.Add(new Shop() { Id = 1, Name = "Test name1" });
    this.unitOfWork.ShopRepository.Add(new Shop() { Id = 2, Name = "Test name2" });
    this.unitOfWork.ShopRepository.Add(new Shop() { Id = 3, Name = "Test name3" });
    this.unitOfWork.ShopRepository.Add(new Shop() { Id = 4, Name = "Test name4" });
    this.unitOfWork.ShopRepository.Add(new Shop() { Id = 5, Name = "Test name5" });
    this.Controller = new ShopController(this.unitOfWork);
}

当我测试GenericRepository的Find-method时,它工作正常。返回正确的Shop对象,我可以断言它工作正常:

    [TestCase]
    public void DetailsReturnsCorrectShop()
    {
      // Arrange
      int testId = 1;
      // Act
      Shop shop = this.unitOfWork.ShopRepository.Find(testId);
      ViewResult result = this.Controller.Details(testId) as ViewResult;
      // Assert
      Shop returnedShop = (Shop)result.Model;
      Assert.AreEqual(testId, returnedShop.Id);
  }

但是当我想测试Get-method从存储库返回所有商店时,如果我不提供任何过滤器参数,我会得到一个空列表。我无法弄清楚为什么?

  [TestCase]
  public void IndexReturnsListOfShops()
  {
      // Arrange
      // Act
      ViewResult result = this.Controller.Index() as ViewResult;
      // Assert
      List<Shop> returnedShops = (List<Shop>)result.Model;
      Assert.AreEqual(5, returnedShops.Count);
  }

ShopController看起来像这样:

public class ShopController : Controller
{
  private readonly IUnitOfWork unitOfWork;
  public ShopController(IUnitOfWork unitOfWork)
  {
      this.unitOfWork = unitOfWork;
  }

  // GET: /Shop/
  public ActionResult Index()
  {
      return View(this.unitOfWork.ShopRepository.Get());
  }

  // GET: /Shop/Details/5
  public ActionResult Details(int? id)
  {
      if (id == null)
      {
          return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }
      Shop shop = this.unitOfWork.ShopRepository.Find(id);
      if (shop == null)
      {
          return HttpNotFound();
      }
      return View(shop);
  }
}

你能帮我弄清楚为什么我从Get-method得到一个空列表?

0 个答案:

没有答案
相关问题