Giter Club home page Giter Club logo

Comments (6)

jsquire avatar jsquire commented on August 20, 2024

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.

github-actions avatar github-actions commented on August 20, 2024

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.

amerjusupovic avatar amerjusupovic commented on August 20, 2024

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.

jsquire avatar jsquire commented on August 20, 2024

@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.

github-actions avatar github-actions commented on August 20, 2024

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.

github-actions avatar github-actions commented on August 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.