我有以下测试:
var home = new DummyPageModel { Parent = null };
var pageRepository = new Mock<IPageRepository>();
pageRepository.Setup(x => x.SingleOrDefault<IPageModel>(page => page.Parent == null)).Returns(home);
但是当我运行此测试时,我收到 System.NotSupportedException :不支持表达式 page => (page.Parent == null)?
SingleOrDefault 的实现如下所示:
public T SingleOrDefault<T>(Func<T, bool> predicate) where T : IPageModel {
return _documentSession.Query<T>().SingleOrDefault(predicate);
}
这样解决:
pageRepository
.Setup(
x => x.SingleOrDefault(
It.Is<Func<IPageModel, bool>>(page => home.Parent == null)
)
)
.Returns(home);