An Xperience project I'm working on contains the following interface:
public interface IContentRepository
{
Task<IEnumerable<T>> GetWebPageAsync<T>(
ContentItemQueryBuilder builder,
Cancellationtoken cancellationToken
) where T : new();
}
The implementation of this is:
public class ContentRepository : IContentRepository
{
private readonly IContentQueryExecutor _contentQueryExecutor;
private readonly IWebPageQueryResultMapper _webPageQueryResultMapper;
private readonly IWebsiteChannelContext _websiteChannelContext;
public ContentRepository(
IContentQueryExecutor contentQueryExecutor,
IWebPageQueryResultMapper webPageQueryResultMapper,
IWebsiteChannelContext websiteChannelContext,
)
{
_contentQueryExecutor = contentQueryExecutor;
_webPageQueryResultMapper = webPageQueryResultMapper;
_websiteChannelContext = websiteChannelContext;
}
public async Task<IEnumerable<T>> GetWebPageAsync<T>(
ContentItemQueryBuilder builder,
CancellationToken cancellationToken
) where T : new()
{
var queryOptions = new ContentQueryExecutionOptions()
{
ForPreview = _websiteChannelContext.IsPreview
};
return await _contentQueryExecutor.GetWebPageResult(
builder: builder,
resultSelector: _webPageQueryResultMapper.Map<T>,
options: queryOptions,
cancellationToken: cancellationToken
);
}
}
The idea behind this repository is that you can just do a single call to get web pages, content and such. For testing the method above I need to mock the _contentQueryExecutor.GetWebPageResult
call to return an IEnumerable
of the given page type. However nothing really seems to work. In my test file for example I do the following:
var homePage = new HomePage
{
SystemFields = new WebPageFields
{
WebPageItemID = 1
}
};
_webPageQueryResultMapper
.Map<HomePage>(Arg.Any<IWebPageContentQueryDataContainer>())
.Returns(homePage);
_contentQueryExecutor
.GetWebPageResult(
builder: Arg.Any<ContentItemQueryBuilder>(),
resultSelector: _webPageQueryResultMapper.Map<HomePage>,
options: Arg.Any<ContentQueryExecutionOptions>(),
cancellationToken: Arg.Any<CancellationToken>()
)
.Returns([homePage]);
But the result always seems to be null
from the _contentQueryExecutor
. The _webPageQueryResultMapper
returns the correct value however. I'm using NSubstitute
and xUnit
for my unit tests.