Yatish
Yatish

Reputation: 155

Unit testing DocumentQuery

I am trying to test a simple query using Kentico UnitTests framework. I have used it in other places, and it seems to have worked but with DocumentQuery no such luck. Nothing complicated. Hope someone can point out what I am missing.

Method under test

  public async Task<IEnumerable<News>> GetNews()    
  {
      var query = new DocumentQuery<News>()
          .FilterDuplicates()
         .OnCurrentSite();

      return await query.GetEnumerableTypedResultAsync();
  }

Unit Test

[TestFixture]  
public class TestClass : UnitTests
{
    [Test]
    public async Task GetNews_ReturnsAllItems()
    {
        //Arrange
        var _fixture = new Fixture();
    
        Fake<SiteInfo, SiteInfoProvider>().WithData(new SiteInfo());
        SiteContext.CurrentSite = _fixture.Create<SiteInfo>();
    
        // Prepares faked data for the Kentico Providers
        DocumentGenerator.RegisterDocumentType<News>(News.CLASS_NAME);
    
        // Fakes the page type to allow creation of fake page data
        Fake().DocumentType<News>(News.CLASS_NAME);
    
        Fake<News>();

        var viewComponent = new NewsScrollViewComponent();
    
        //Act
        var result = await viewComponent.GetNews();
    
        //Assert
    }
}

And it's failing on the last line where it's trying to get the typed results from the query. It is trying to connect to database running the actual query instead of using the Fakes.

The connection property has not been initialized

What am I missing?

Upvotes: 0

Views: 56

Answers (1)

Yatish
Yatish

Reputation: 155

According to Kentico support, DocumentQuery can't be Faked(using Kentico's UnitTests framework). It is however can be mocked using any mocking framework.

It is slightly confusing because they propose to use Kentico Fake for their providers which are higher level apis and in turn will be calling something like ObjectQuery

Upvotes: 0

Related Questions