【发布时间】:2012-11-01 13:34:01
【问题描述】:
使用 Microsoft fakes 我在我的存根对象中有以下方法签名:
public void GetAllOf1ExpressionOfFuncOfT0M0Int32Int32<T2>(FakesDelegates.Func<Expression<System.Func<T, T2>>, int, int, IList<T>> stub);
这是这个真实的存根方法:
IList<T> GetAll<T2>(Expression<Func<T, T2>> orderbyexpr, int nStartAt = -1, int NumToSel = -1);
我想做的是通过使用此方法为存根方法分配自定义内容:
public static RunReport StubMethod<T>(ref FakesDelegates.Func<T> func, T returnValue)
{
var counter = new RunReport();
func = () =>
{
Interlocked.Increment(ref counter.Count);
return returnValue;
};
return counter;
}
我的问题是我无法理解 StubMethod 的签名应该是什么以及如何调用它?
我尝试了一些导致“方法组无法分配/无法转换方法组”的事情。
顺便说一句 - 它与其他更简单的存根方法完美配合:
IList<T> IRepository<T>.GetAll();
所以一定是定义问题...
这是我在代码中使用 GetAll 的地方...这应该重定向到我的自定义函数:
public IList<EntityType> GetAllEntityTypesByName(string filter)
{
IList<EntityType> types = new List<EntityType>();
try
{
using (IUnitOfWork uow = ctx.CreateUnitOfWork(true))
{
IRepository<EntityType> repType = uow.CreateRepository<EntityType>();
if (string.IsNullOrEmpty(filter))
{
types = repType.GetAll(o => o.Name);
}
else
{
types = repType.Find(t => t.Name.ToLower().Contains(filter.ToLower()), o => o.Name);
}
}
}
catch (Exception e)
{
ResourceManager.Instance.Logger.LogException(e);
}
return types;
}
另一种看待问题的方式是我想替换这个:
stubRepType.GetAllOf1ExpressionOfFuncOfT0M0Int32Int32<string>((a, b, c) => { return new List<EntityType>(); });
用这个:
TestHelper.StubRepositoryMethod<IList<EntityType>>(ref stubRepType.GetAllOf1ExpressionOfFuncOfT0M0Int32Int32<string>, new List<EntityType>());
【问题讨论】:
-
解决方案中第七个文件第二行的第四个字符(按第七个字符排序,然后按最后编辑日期排序)是邪恶的。除了有趣之外,如果不查看实际调用 StubMethod 的代码,您的问题几乎是不可能解决的。
标签: c# generics delegates stub microsoft-fakes