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

[Improvement]: Test Double approach doesn't work as expected for function calls with path parameters #40810

Closed
DimuthuMadushan opened this issue Jun 22, 2023 · 2 comments
Assignees
Labels
Area/TestFramework Team/DevTools Ballerina Developer Tooling ( CLI, Test FW, Package Management, OpenAPI, APIDocs ) Type/Improvement

Comments

@DimuthuMadushan
Copy link
Contributor

Description

Trying to create a test double for githubRestClient in this example. Since the example include the function call with path parameters(self.githubRestClient->/users/[owner]/repos;), I have used the following approach with appropriate return values.

final http:Client githubRestClient = check initRestClient();
function initRestClient() returns http:Client|error => check new ("https://api.github.com", {auth: {token: authToken}});

public client class MockHttpClient {

    remote function get(string path, map<string|string[]>? headers = (), http:TargetType targetType = http:Response) returns http:Response|anydata|http:ClientError {
    }

    resource function get [string... path](map<string|string[]>? headers = (), http:TargetType targetType = http:Response, *http:QueryParams params) returns http:Response|anydata {
    }

    remote function post (string path, http:RequestMessage message, map<string|string[]>? headers = (), string? mediaType = (), http:TargetType targetType = http:Response) returns http:Response|anydata|http:ClientError {
    }
}

@test:Mock {
    functionName: "initRestClient"
}
function initMockRestClient() returns http:Client|error {
    return test:mock(http:Client, new MockHttpClient());
}

When running the bal test command, it returns error: there are test failures.

Describe your problem(s)

No response

Describe your solution(s)

No response

Related area

-> Test Framework

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@Dilhasha
Copy link
Contributor

Dilhasha commented Jul 6, 2023

Update:

public client class MockHttpClient {

    isolated resource function get [http:PathParamType... path](map<string|string[]>? headers = (), http:TargetType targetType = http:Response,
            *http:QueryParams params) returns http:Response|anydata|http:ClientError {
        GitHubRepository repo = {
            id: 1,
            name: "Test-Repo",
            'fork: false,
            created_at: "2020-10-10T10:10:10Z",
            updated_at: "2020-10-10T10:10:10Z",
            language: "Ballerina",
            has_issues: false,
            forks_count: 5,
            open_issues_count: 9,
            visibility: "Public",
            forks: 7,
            open_issues: 3,
            watchers: 55,
            default_branch: "master"
        };
        if path.toString().startsWith("/user") {
            GitHubUser user = {
                id: 1233,
                login: "balUser",
                url: "https://github.com/gitapi/users/balUser",
                created_at: "2020-10-10T10:10:10Z",
                updated_at: "2020-10-10T10:10:10Z",
                avatar_url: "https://avatars0.githubusercontent.com/u/1233?v=4",
                'type: "User",
                blog: "https://ballerina.io",
                company: "WSO2",
                name: "Ballerina User",
                location: "Sri Lanka"
            };
            return user;
        }
        if path.toString().startsWith("/users") && path.toString().endsWith("/repos") {
            return [repo];
        }
        if path.toString().startsWith("/repos") {
            if path.toString().endsWith("/issues") {
                Issue issue = {
                    number: 123
                };
                return issue;
            }
            return repo;
        }
        return error("error while mocking the response");
    }

    remote function post(string path, http:RequestMessage message, map<string|string[]>? headers = (), string? mediaType = (), http:TargetType targetType = http:Response)
            returns http:Response|anydata|http:ClientError {
        GitHubRepository expectedResult = {
            id: 1,
            name: "Test-Repo",
            'fork: false,
            created_at: "2020-10-10T10:10:10Z",
            updated_at: "2020-10-10T10:10:10Z",
            language: "Ballerina",
            has_issues: false,
            forks_count: 5,
            open_issues_count: 9,
            visibility: "Public",
            forks: 7,
            open_issues: 3,
            watchers: 55,
            default_branch: "master"
        };
        return expectedResult;
    }

}

@Dilhasha
Copy link
Contributor

Based on above, it is error handling that is not working as expected and will be fixed with #40934

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/TestFramework Team/DevTools Ballerina Developer Tooling ( CLI, Test FW, Package Management, OpenAPI, APIDocs ) Type/Improvement
Projects
Archived in project
Development

No branches or pull requests

3 participants