Search code examples
nunitmocking

How do I setup this (Mock setup)


I have the following test:

        var home = new DummyPageModel { Parent = null };
        var pageRepository = new Mock<IPageRepository>();
        pageRepository.Setup(x => x.SingleOrDefault<IPageModel>(page => page.Parent == null)).Returns(home);

but when I run this test I get a System.NotSupportedException : Expression page => (page.Parent == null) is not supported?

The implementation of SingleOrDefault looks like this:

    public T SingleOrDefault<T>(Func<T, bool> predicate) where T : IPageModel {
        return _documentSession.Query<T>().SingleOrDefault(predicate);
    }

Solution

  • Solved it like this:

    pageRepository
        .Setup(
            x => x.SingleOrDefault(
                It.Is<Func<IPageModel, bool>>(page => home.Parent == null)
            )
        )
        .Returns(home);