Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT]: Better mocking support for GitHubClient #129

Closed
1 task done
StevenJDH opened this issue Oct 5, 2024 · 3 comments
Closed
1 task done

[FEAT]: Better mocking support for GitHubClient #129

StevenJDH opened this issue Oct 5, 2024 · 3 comments
Labels
Type: Feature New feature or request

Comments

@StevenJDH
Copy link

Describe the need

I recently had a requirement to mock the GitHubClient that is injected into my service class in order to test the business logic of that class. However, when using Moq, I encountered the message about not being able to mock because of non-overridable members, in this case, WithRefItemRequestBuilder.GetAsync. Here is one example under .NET 8:

var gitRef = new GitRef
{
    Ref = "refs/heads/test"
};

var tokenProvider = new BaseBearerTokenAuthenticationProvider(new TokenProvider("123456"));
var mockGitHub = new Mock<GitHubClient>(RequestAdapter.Create(tokenProvider))

mockGitHub.Setup(x => x.Repos[It.IsAny<string>()][It.IsAny<string>()].Git.MatchingRefs[It.IsAny<string>()]
                .GetAsync(It.IsAny<Action<RequestConfiguration<DefaultQueryParameters>>?>(), It.IsAny<CancellationToken>()))
            .ReturnsAsync([gitRef]);

It's a bit verbose due to optional parameters having to be specified, but essentially, the GetAsync method, which comes from GitHub.Repos.Item.Item.Git.MatchingRefs.Item.WithRefItemRequestBuilder is the culprit. I've also tried instead mocking the adapter since this method calls RequestAdapter.SendCollectionAsync<GitRef>() under the hood, but either it wasn't working or I did it wrong.

My wish is that there be an IGitHubClient or other type of interface that could be used for testing, or make the methods virtual so that they were more test friendly.

SDK Version

octokit/dotnet-sdk v0.0.28

API Version

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@StevenJDH StevenJDH added Status: Triage This is being looked at and prioritized Type: Feature New feature or request labels Oct 5, 2024
Copy link

github-actions bot commented Oct 5, 2024

👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labeled with Status: Up for grabs. You & others like you are the reason all of this works! So thank you & happy coding! 🚀

@kfcampbell
Copy link
Member

Hmm...this is a great question. Perhaps it's something that should be filed in the Kiota repo, as our client is generated using the Kiota client. I wonder how they'd recommend mocking and testing.

Of course, there's always the idea to introduce a layer of abstraction in your application and mock that, but I wonder what the Kiota team would suggest.

@kfcampbell kfcampbell removed the Status: Triage This is being looked at and prioritized label Oct 7, 2024
@StevenJDH
Copy link
Author

Hmm...this is a great question. Perhaps it's something that should be filed in the Kiota repo, as our client is generated using the Kiota client. I wonder how they'd recommend mocking and testing.

Of course, there's always the idea to introduce a layer of abstraction in your application and mock that, but I wonder what the Kiota team would suggest.

Makes sense now that you mention it. I've raised a new feature request for this with them now under microsoft/kiota#5560 to continue the discussion there. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature New feature or request
Projects
Archived in project
Development

No branches or pull requests

2 participants