0

I'm trying to mock the return set in tableClient.QueryAsycn<TableEntity>(), but it continues to return empty lists. Is there anything glaringly obvious that I'm missing in my unit test.

The method simply calls QueryAsync() and returns a List<String>().

I have looked at an example on StackExchange:

How to create an Azure.AsyncPagealb for mocking?

as well as other examples, but I'm still not able to mock the tableClient.QueryAsync<TableEntity>() successfully.

I can make it through the method successfully, but nothing I try seems to make the method work. Any thoughts?

 [Fact]
 public async Task GetAllSettingsAsync_ShouldReturnListOfStrings()
 {
     // Arrange
     var fakeConfiguration = A.Fake<IConfiguration>();
     var fakeTableServiceClient = A.Fake<ITableFactoryService>();
     var fakeTableClient = A.Fake<TableClient>();

     Page<TableEntity> page = Page<TableEntity>.FromValues(
         new List<TableEntity>
         {
             new TableEntity("p1", "k1"),
             new TableEntity("p2", "k2"),
             new TableEntity("p3", "k3"),
         },
         continuationToken: null,
         A.Fake<Response>());

     AsyncPageable<TableEntity> tables = AsyncPageable<TableEntity>.FromPages(new[] { page });

     A.CallTo(() => fakeTableClient.QueryAsync<TableEntity>(A<string>.That.Matches(s => s == "PartitionKey eq 'default'"),
                                    A<int>._,
                                    A<IEnumerable<string>>._,
                                    A<CancellationToken>._)).Returns(tables);
     var cut = new SettingsRepo(fakeConfiguration, fakeTableServiceClient);

     // Act 
     var result = await cut.GetAllSettingsAsync();

     // Assert 
     Assert.True(result.Count > 0);
}
1
  • 1
    You created a fake TableClient, but you never passed it to the SUT, so it's never used. Commented Aug 14 at 21:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.