【发布时间】:2013-11-09 17:22:27
【问题描述】:
我一直在尝试弄清楚如何对我的应用程序进行单元测试,但我不太了解所有内容如何组合在一起。
我遵循了 John Papa 的 PluralSight (SPA) 教程,并以完全相同的方式构建了我的模型、存储库和工作单元。不幸的是,他没有提供任何关于我们如何进行单元测试的示例。
我玩过 Moq 并在网上找到了很少的链接来解释如何做到这一点,但不幸的是我一无所获。
一些提供上下文的代码:
public interface IRepository<T> where T : class
{
IQueryable<T> GetAll();
T GetById(int id);
void Add(T entity);
void Update(T entity);
void Delete(T entity);
void Delete(int id);
}
public interface IFeedbackRepository : IRepository<Feedback>
{
IQueryable<Feedback> GetByFeedbackFor(int id);
}
public class FeedbackRepository : EFRepository<Feedback>, IFeedbackRepository
{
public FeedbackRepository(WebAppDbContext context) : base(context) { }
public IQueryable<Feedback> GetByFeedbackFor(int id)
{
return DbSet.Where(f => f.FeedbackForId == id);
}
}
public interface IWebAppUow
{
void Commit();
IFeedbackRepository Feedbacks { get; }
}
public void TestMethod1()
{
Mock<IWebAppUow> mockUnitOfWork = new Mock<IWebAppUow>();
// THEN ??
}
编辑:我找到了这个链接 (http://msdn.microsoft.com/en-us/data/dn314429.aspx),它解释了如何做,但直接在 DbSet 上工作。如果有人可以解释我们如何修改此示例以使用 UoW 和存储库模式,那就太好了!
【问题讨论】:
-
如果你要测试一些使用它的代码,你需要模拟
IWebAppUow。因此,如果没有看到您想要测试的实际代码,就很难回答您的问题。 -
假设我的 FeedbackRepository 中有以下内容: public Feedback GetByIds(int feedbackForId, int feedbackFromId) { return DbSet.FirstOrDefault( f => f.FeedbackForId == feedbackForId && f.FeedbackFromId == feedbackFromId ); }
-
那么您可能想要模拟您的存储库。
标签: c# asp.net-mvc entity-framework unit-testing moq