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

Mocking data for unit testing. #275

Open
EvilKot opened this issue Oct 18, 2024 · 3 comments
Open

Mocking data for unit testing. #275

EvilKot opened this issue Oct 18, 2024 · 3 comments

Comments

@EvilKot
Copy link
Contributor

EvilKot commented Oct 18, 2024

Hello 👋

In current implementation making API calls is done through methods in ApiExecutor classes, such as SourceFilesApiExecutor, SourceStringsApiExecutor, etc.

Example:

ICrowdinApiClient client = /* Initialize client */

var sourceFiles = await client.SourceFiles.ListFiles<FileInfoCollectionResource>(1, 25, 0);
var sourceStrings = await client.SourceStrings.ListStrings(1, 25, 0);

Since ICrowdinApiClient defines executor properties as concrete classes and those classes have non-virtual methods, it's not possible to mock a data returned by a client for unit testing purposes.

Moreover, since send request methods of the client are marked as internal, it's also not possible to at least mock the returned JSON, similarly to how it's done in Crowdin client unit tests.

Desired use case example:

using Moq;
using Crowdin.Api;

// ...

private Mock<ICrowdinApiClient> _clientMock;

[SetUp]
public void Setup()
{
    _clientMock = new Mock<ICrowdinApiClient>();
}

[Test]
public async Task ShouldPass()
{
    var expectedResponse = /* Assign data */
    _clientMock.Setup(client => client.SourceStrings.ListStrings(/* setup */).ReturnsAsync(expectedResponse );
                                                 ^^^^
                                                 Moq - Non-overridable members may not be used in setup / verification expressions

One of suggestions is to define interface for every ApiExecturor class or simply mark methods as virtual.
Current workaround is to define a wrapper class and proxy calls to Crowdin Api client.

WDYT?

@andrii-bodnar
Copy link
Member

@innomaxx please take a look

@innomaxx
Copy link
Collaborator

innomaxx commented Nov 7, 2024

Hi @EvilKot. Good idea!

One of suggestions is to define interface for every ApiExecutor class or simply mark methods as virtual.

I suggest the 1st approach: define an interface for each API Executor. It's not so time-consuming, I can deal with it soon.

@andrii-bodnar what's your opinion?

@andrii-bodnar
Copy link
Member

Sounds good to me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants