C# 具有接受谓词的泛型方法的存根接口
我真的很难让它发挥作用。我有一个通用的存储库模式,我需要使用Microsoft Fakes存根存储库接口C# 具有接受谓词的泛型方法的存根接口,c#,lambda,microsoft-fakes,C#,Lambda,Microsoft Fakes,我真的很难让它发挥作用。我有一个通用的存储库模式,我需要使用Microsoft Fakes存根存储库接口 public interface IDataAccess<T> where T : class { void Add(T entity); void Update(T entity); void Delete(T entity); void Delete(Expression<Func<T, bool>> where);
public interface IDataAccess<T> where T : class
{
void Add(T entity);
void Update(T entity);
void Delete(T entity);
void Delete(Expression<Func<T, bool>> where);
T FindById(long id);
T FindById(string id);
T Find(Expression<Func<T, bool>> where);
IEnumerable<T> FindAll();
IEnumerable<T> FindMany(Expression<Func<T, bool>> where);
IQueryable<T> Find(Expression<Func<T, bool>> predicate = null, params Expression<Func<T, object>>[] includes);
IQueryable<T> FindIncluding(params Expression<Func<T, object>>[] includeProperties);
}
公共接口IDataAccess,其中T:class
{
无效添加(T实体);
无效更新(T实体);
无效删除(T实体);
作废删除(如有);
T FindById(长id);
T FindById(字符串id);
找不到(表达式在何处);
IEnumerable FindAll();
IEnumerable FindMany(表达式,其中);
IQueryable Find(表达式谓词=null,参数表达式[]包含);
可查询的FindIncluding(参数表达式[]包含属性);
}
正在尝试为创建存根
IQueryable<T> Find(Expression<Func<T, bool>> predicate = null, params Expression<Func<T, object>>[] includes);
IQueryable<T> FindIncluding(params Expression<Func<T, object>>[] includeProperties);
IQueryable Find(表达式谓词=null,参数表达式[]包含);
可查询的FindIncluding(参数表达式[]包含属性);
在我的测试中
IDataAccess<EnterprisePermissionSet> dataAccess = new HG.Fus.Authentication.Data.Fakes.
StubIDataAccess<EnterprisePermissionSet>()
{
FindIncludingExpressionOfFuncOfT0ObjectArray = () => { };
};
IDataAccess dataAccess=new HG.Fus.Authentication.Data.Fakes。
StubIDataAccess()
{
FindIncludingExpressionOfFuncOfT0ObjectArray=()=>{};
};
我只是不知道如何构造这个存根,
MsFakes
使用代码生成来替换假方法、创建存根等等。。。
要替换特定方法,必须设置新方法(Action
或Func
,基于方法签名),该方法将接收对此特定方法的任何调用
要伪造的方法的签名为:
IQueryable<T> FindIncluding(params Expression<Func<T, object>>[] includeProperties);
模拟IQueryable
的最简单方法是通过AsQueryable()
返回集合,另一种方法是使用EnumerableQuery
类
下面是要替换的示例:IQueryable Find(表达式谓词=null,参数表达式[]包含)代码>
fakeDataAccess.FindExpressionOfFuncOfT0BooleanExpressionOfFuncOfT0ObjectArray=
(表达式,表达式)=>
{
//在这里,您可以创建逻辑/填充返回值等。。。
返回新列表().AsQueryable();
};
你必须模拟你在{}
中寻找的行为,这就是我最初所做的,但不断得到一个错误,我无法将源类型“Lambda Expression”转换为目标类型“IQueryable”好的,阅读我的答案,我展示了存根这些方法的方法。。。
fakeDataAccess.FindIncludingExpressionOfFuncOfT0ObjectArray = expressions =>
{
// here you can create the logic / fill the return value and etc...
return new List<EnterprisePermissionSet>().AsQueryable(); \\this example is match your T
};
fakeDataAccess.FindExpressionOfFuncOfT0BooleanExpressionOfFuncOfT0ObjectArray =
(expression, expressions) =>
{
// here you can create the logic / fill the return value and etc...
return new List<EnterprisePermissionSet>().AsQueryable();
};