question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cannot mock TableQuerySegment

See original GitHub issue

I’m writing unit tests for my codes where I need to fake Azure storage. Inside my test codes, I need to mock cloudTable.ExecuteQuerySegmentedAsync(query, continuationToken, cancellationToken) which returns TableQuerySegment<TElement>. However TableQuerySegment has no public contructors so I’m unable to mock it or consturct its instance. What can be done so that I don’t have to call azure actually but get a fake data to continue my tests?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

40reactions
pdl5pcommented, Feb 8, 2018

I’ve worked around this issue by using reflection to create a mock (well actually a real) instance of TableQuerySegment like so:-

var ctor = typeof(TableQuerySegment<MyEntity>)
                .GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)
                .FirstOrDefault(c => c.GetParameters().Count() == 1);

var mockQuerySegment = ctor.Invoke(new object[] { new List<MyEntity>() }) as TableQuerySegment<MyEntity>;

9reactions
ljsgcommented, Jun 12, 2020

I’ve worked around this issue by using reflection to create a mock (well actually a real) instance of TableQuerySegment like so:-

var ctor = typeof(TableQuerySegment<MyEntity>)
                .GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)
                .FirstOrDefault(c => c.GetParameters().Count() == 1);

var mockQuerySegment = ctor.Invoke(new object[] { new List<MyEntity>() }) as TableQuerySegment<MyEntity>;

If like me you also need to use the ContinuationToken in your tests you can expand @pdl5p 's code like so:

MethodInfo setTokenMethod = typeof(TableQuerySegment<MyEntity>).GetMethod("set_ContinuationToken", BindingFlags.NonPublic | BindingFlags.Instance);

var continuationToken = new TableContinuationToken();
setTokenMethod.Invoke(mockQuerySegment , new object[] { continuationToken });

cloudTable.Setup(t => t.ExecuteQuerySegmentedAsync(It.IsAny<TableQuery<MyEntity>>(), It.IsAny<TableContinuationToken>()))
                    .Returns(Task.FromResult(mockQuerySegment ))

This can be used to test when ExecuteQuerySegmentedAsync returns a continuation token meaning more results are available from the table

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking CloudStorageAccount and CloudTable for Azure ...
My goal is to mock it, but since it is virtual and doesn't implement any interface, I can't come over with a simple...
Read more >
Developers - cannot mock TableQuerySegment -
I'm writing unit tests for my codes where I need to fake Azure storage. Inside my test codes, I need to mock cloudTable....
Read more >
TableQuerySegment<TElement> Class
Represents a segment of results and contains continuation token information.
Read more >
Unit testing and mocking with Azure SDK .NET
We'll illustrate this set of examples using a popular .NET mocking framework, Moq. To create a test client instance using Moq: Mock< ...
Read more >
Doing async the right way
The problem with the methods above is that although they return control to the caller ... AddRange(await FetchRecords(table, query, segment.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found