對於我的單元測試,我需要僞造一個存儲庫。我很容易就能僞造除了將Linq Expression委託作爲參數的Find方法以外的所有方法。僞造存儲庫 - 僞造查找方法
我的假存儲庫代碼列在下面(不必要的代碼已刪除)。我嘗試使用的代碼顯示在Find方法中。我從VS獲得的編譯器錯誤是:
「System.Collections.Generic.List」不包含'Where'和最佳擴展方法重載定義'System.Linq.Queryable.Where(System.Linq .IQueryable,System.Linq.Expressions.Expression>)」有一些無效參數」
如何我彎曲標準參數進入參數類型所需的任何想法?
public class FakeCourseRepository : IRepository<Course>
{
private List<Course> courseList;
public FakeCourseRepository(List<Course> courses)
{
courseList = courses;
}
public IList<Course> Find(System.Linq.Expressions.Expression<Func<Course, bool>> criteria)
{
return courseList.Where<Course>(criteria);
}
}
感謝KeithS,這是一個非常有用的答案。我還會圍繞一個具體的列表來研究你的建議。 – Ozzy 2010-11-05 09:14:25