Comments (6)
Hi @amerjusupovic. Can you help me understand the end-to-end scenario of what you're trying to test?
Unless you've got an unusual scenario, you shouldn't need to mock the extensions, but would be mocking the client method to return your own pagable, in a pattern similar to what is demonstrated in [Unit testing and mocking with the Azure SDK for .NET](https://learn.microsoft.com/dotnet/azure/sdk/unit-testing-mocking?tabs=moq#explore-paging:
Page<SecretProperties> page1 = Page<SecretProperties>.FromValues(
new[]
{
new SecretProperties("secret1"),
new SecretProperties("secret2")
},
"continuationToken",
Mock.Of<Response>());
Page<SecretProperties> page2 = Page<SecretProperties>.FromValues(
new[]
{
new SecretProperties("secret3"),
new SecretProperties("secret4")
},
"continuationToken2",
Mock.Of<Response>());
Page<SecretProperties> lastPage = Page<SecretProperties>.FromValues(
new[]
{
new SecretProperties("secret5"),
new SecretProperties("secret6")
},
continuationToken: null,
Mock.Of<Response>());
Pageable<SecretProperties> pageable = Pageable<SecretProperties>
.FromPages(new[] { page1, page2, lastPage });
AsyncPageable<SecretProperties> asyncPageable = AsyncPageable<SecretProperties>
.FromPages(new[] { page1, page2, lastPage });
from azure-sdk-for-net.
Hi @amerjusupovic. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.
from azure-sdk-for-net.
Thanks, my scenario is that I want to test my code that calls ConfigurationClient.GetConfigurationSettingsAsync.AsPages(IEnumerable<MatchConditions>)
. Before, I only had calls to AsPages()
without any params and that was fine because I could create a mock of AsyncPageable<ConfigurationSetting>
and implement a mock AsPages
method. However, that doesn't work anymore because I'm not allowed to mock an extension method in Moq
and I can't just create a new AsPages
on the mock pageable like I have before, since it won't be an extension.
For example, I can't do this:
var mockClient = new Mock<ConfigurationClient>(MockBehavior.Strict);
mockClient.Setup(c => c.GetConfigurationSettingsAsync(
It.IsAny<SettingSelector>(),
It.IsAny<CancellationToken>())
.AsPages(
It.IsAny<IEnumerable<MatchConditions>>(),
It.IsAny<string>(),
It.IsAny<int>()))
// rest of setup
and I can't just mock GetConfigurationSettingsAsync
because when it calls the AsPages(IEnumerable<MatchConditions>)
extension method, it throws InvalidOperationException
.
Let me know if that makes sense or if there's a different way to test the extension.
from azure-sdk-for-net.
@amerjusupovic: In this case, you'd want to combine the approach that I described above along with a custom mock extension method. By defining your extension method in the same namespace as the code calling it and using actual types rather than open generics, it will be the implementation that the compiler prefers.
for example:
Page<ConfigurationSetting> page1 = Page<ConfigurationSetting>.FromValues(
new[]
{
new ConfigurationSetting("secret1", "val1"),
new ConfigurationSetting("secret2", "val2")
},
"continuationToken",
Mock.Of<Response>());
Page<ConfigurationSetting> page2 = Page<ConfigurationSetting>.FromValues(
new[]
{
new ConfigurationSetting("secret3", "val3"),
new ConfigurationSetting("secret4", "val4")
},
"continuationToken2",
Mock.Of<Response>());
Page<ConfigurationSetting> lastPage = Page<ConfigurationSetting>.FromValues(
new[]
{
new ConfigurationSetting("secret5", "val5"),
new ConfigurationSetting("secret6", "val6")
},
continuationToken: null,
Mock.Of<Response>());
AsyncPageable<ConfigurationSetting> asyncPageable = AsyncPageable<ConfigurationSetting>
.FromPages(new[] { page1, page2, lastPage });
var mockClient = new Mock<ConfigurationClient>();
mockClient
.Setup(c => c.GetConfigurationSettingsAsync(
It.IsAny<SettingSelector>(),
It.IsAny<CancellationToken>()))
.Returns(asyncPageable);
// Invoke the mocks
var mockPagable = mockClient.Object.GetConfigurationSettingsAsync(new(), default);
var mockPages = mockPagable.AsPages(new[] { new MatchConditions() });
// Mock extensions with more specific typing and namespace locality.
public static class MockExtensions
{
public static async IAsyncEnumerable<Page<ConfigurationSetting>> AsPages(
this AsyncPageable<ConfigurationSetting> instance,
IEnumerable<MatchConditions> matches,
string continuationToken = null,
int? pageHint = null)
{
await foreach (var page in instance.AsPages())
{
yield return page;
}
}
}
from azure-sdk-for-net.
Hi @amerjusupovic. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation.
from azure-sdk-for-net.
Hi @amerjusupovic, since you haven’t asked that we /unresolve
the issue, we’ll close this out. If you believe further discussion is needed, please add a comment /unresolve
to reopen the issue.
from azure-sdk-for-net.
Related Issues (20)
- [DocumentIntelligence] Re-enable GetAnalyzeResultPdf once service issue is fixed HOT 1
- [DocumentIntelligence] Re-enable CopyClassifierTo tests
- 90% of the ServiceBusReceiver.CloseAsync() calls throws "System.TimeoutException: The operation did not complete within the allocated time 00:01:00 for object drain" HOT 15
- [QUERY] Connecting to ACR using Managed Identity to create ACI HOT 1
- Regenerate Azure AI Search with latest Swagger
- [BUG] UploadAsync() chooses block IDs that can result in a corrupt upload when others occur at same time HOT 2
- [BUG] Cannot fetch secrets HOT 2
- [BUG] Method overload `BlobBaseClient.OpenReadAsync(bool allowBlobModifications,...)` incorrectly handles the `allowBlobModifications` flag HOT 1
- [BACKLOG] Azure Monitor Exporter: remove Http pre-stable attributes HOT 1
- [QUERY][DNS]Retry policy for GetAllRecordDataAyns() HOT 1
- Threshold is missing on VectorizedQuery HOT 3
- Release new version for ConnectedVMwarevSphere
- [FEATURE REQ] - Allow providing BlobDeleteType when deleting a blob via BlobClient HOT 1
- [QUERY] DefaultIdentityNotDefined error response from attempt to get User-assigned MSI token from a scope HOT 5
- [BUG] Spec repo azure-sdk-for-net-track2 CI failed for new RP
- [BUG] Facing issue with chat completion streaming option using GPT4O and GPT4V model in Azure SDK V2 HOT 1
- [BUG] Azure.ResourceManager.Quota CreateOrUpdate fails when WaitUntil.Completed is set HOT 1
- [FEATURE REQ] Support for Azure DevOps Workload Identity HOT 1
- [BUG] Metrics(Requests,Dependencies) was sampled when moved to AzureMonitorOpenTelemetry HOT 1
- [BUG] StopProcessingAsync on ServiceBusProcessor exported to Azure Application insights as a Dependency failure HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from azure-sdk-for-net.