我试图用NSubstitute来模拟替代品的返回值,但我无法获得替代品以返回正确的值,因为方法签名使用的是Func。Func与NSubstitute的模拟结果
我见过这些问题,但无法使它与我的Func一起工作。
Mocking Action<T> with NSubstitute
Mocking out expression with NSubstitute
我试图嘲弄的接口是这个(有点simplyfied):
public interface IOrgTreeRepository<out T> where T : IHierarchicalUnit
{
T FirstOrDefault(Func<T, bool> predicate);
}
我与NSubstitute取代它像这样:
_orgTreeRepository = Substitute.For<IOrgTreeRepository<IOrganizationUnit>>();
然后我尝试改变返回v区实习像这样:
_orgTreeRepository.FirstOrDefault(Arg.Is<Func<IOrganizationUnit, bool>>(x => x.Id== _itemsToUpdate[0].Id)).Returns(existingItems[0]);
但它只是返回一个代理对象,而不是在existingItems我定义的对象。
但是,由于其他问题,我设法使这个工作,但它并没有帮助我,因为我每次都需要一个特定的项目。
_orgTreeRepository.FirstOrDefault(Arg.Any<Func<IOrganizationUnit, bool>>()).Returns(existingItems[0]); // Semi-working
我想这是治疗lambda表达式作为一种绝对参考,因此跳过它?有什么办法可以嘲笑返回值吗?
那么,这是一个很好的解决我的问题。非常感谢。 – smoksnes
我从来没有猜到过!万分感谢。 – Invvard