Closed niveathika closed 1 month ago
I was able to reproduce the mentioned error with the following example.
main.bal
import ballerina/http;
import ballerina/regex;
http:Client clientEndpoint = check new ("https://api.chucknorris.io/jokes/");
type Joke readonly & record {|
string value;
|};
// This function performs a `get` request to the Chuck Norris API and returns a random joke
// with the name replaced by the provided name or an error if the API invocation fails.
function getRandomJoke(string name) returns string|error {
Joke joke = check clientEndpoint->get("/random");
string replacedText = regex:replaceAll(joke.value, "Chuck Norris", name);
return replacedText;
}
main_test.bal
import ballerina/test;
import ballerina/http;
// An instance of this object can be used as the test double for the `clientEndpoint`.
public client class MockHttpClient {
remote function get(string path, map<string|string[]>? headers = (), http:TargetType targetType = http:Response) returns http:Response|anydata|http:ClientError {
// Joke joke = {"value": "Mock When Chuck Norris wants an egg, he cracks open a chicken."};
return {"value": "Mock When Chuck Norris wants an egg, he cracks open a chicken."};
}
}
@test:Config {}
public function testGetRandomJoke() {
// create and assign a test double to the `clientEndpoint` object
clientEndpoint = test:mock(http:Client, new MockHttpClient());
// invoke the function to test
string|error result = getRandomJoke("Sheldon");
// verify that the function returns the mock value after replacing the name
test:assertEquals(result, "Mock When Sheldon wants an egg, he cracks open a chicken.");
}
The issue occurs when you try to return the record directly as follows.
return {"value": "Mock When Chuck Norris wants an egg, he cracks open a chicken."};
But if you assign it to a variable and return, this error can be avoided.
Joke joke = {"value": "Mock When Chuck Norris wants an egg, he cracks open a chicken."};
return joke;
@niveathika Until we fix the issue can you use it as a workaround?
Ack. I will use this workaround for the time being
Faced a similar issue when mocking an http:Client with a post()
. Assigning and returning worked fine, but we might at least need to document this.
Closing this since a workaround is provided.
This issue is NOT closed with a proper Reason/ label. Make sure to add proper reason label before closing. Please add or leave a comment with the proper reason label now.
- Reason/EngineeringMistake - The issue occurred due to a mistake made in the past.
- Reason/Regression - The issue has introduced a regression.
- Reason/MultipleComponentInteraction - Issue occured due to interactions in multiple components.
- Reason/Complex - Issue occurred due to complex scenario.
- Reason/Invalid - Issue is invalid.
- Reason/Other - None of the above cases.
Description
The code given in https://ballerina.io/learn/test-ballerina-code/mocking/#create-a-test-double is not working.
The above results in
This results in following error,
Steps to Reproduce
No response
Affected Version(s)
No response
OS, DB, other environment details and versions
No response
Related area
-> Test Framework
Related issue(s) (optional)
No response
Suggested label(s) (optional)
No response
Suggested assignee(s) (optional)
No response