Capture Mock Data
- 11 Jan 2023
-
DarkLight
-
PDF
Capture Mock Data
- Updated on 11 Jan 2023
-
DarkLight
-
PDF
Article summary
Did you find this summary helpful?
Thank you for your feedback
Capture Mock Data from Postman
This image illustrates the response body when mock data is captured using Postman.
Capture Mock Data from Test
- When the
isSaveMockData
flag is set totrue
(default is false) and thestub
flag is set tofalse
(default is true) in the adapterTestIntegration.js file, and you run a true integration test, it will make all API calls to the other system and save the responses into mock data files. It will also edit the appropriate action.json file so that the next time a call is made in standalone mode, it will return the new mock data. - The saveMockData method is already in the individual calls built by the Adapter Builder. This method does not do anything unless the two flags are set properly.
Example: Adaptertestintegration.js
const isSaveMockData = true;
const stub = false;
...
describe ('#createDeployment-errors', () => {
it('should work if integrated or standalone with mockdata', (done) => {
try {
a.createDeployment(apisApiId, apisCreateDeploymentBodyParam, null,
(data, error) => {
try {
runCommonAsserts(data, error);
if (stub) {
assert.equal('string', data.response.CreatedDate);
assert.equal('string', data.response.DeploymentId);
...
} else {
assert.equal('string', data.response.CreatedDate);
}
saveMockData('Apis', 'createDeployment', 'default', data);
done();
} catch (err) {
log.error(`Test Failure: ${err}`);
done(err);
}
});
} catch (error) {
log.error(`Adapter Exception: ${error}`);
done(error);
}
}).timeout(attemptTimeout);
});
- There is another property that is also in the request section
– return_raw
. This property makes the raw response from the other system available in the result that was returned. ThesaveMockData
method will use this data if it is available since it has not been translated. In contrast, if you are using translation,saveMockData
will save the translated response, and you will then need to edit the data and revert the translation.
Example: Adaptertestintegration.js
return_raw: true,
...
describe ('#createDeployment-errors', () => {
it('should work if integrated or standalone with mockdata', (done) => {
try {
a.createDeployment(apisApiId, apisCreateDeploymentBodyParam, null,
(data, error) => {
try {
runCommonAsserts(data, error);
if (stub) {
assert.equal('string', data.response.CreatedDate);
assert.equal('string', data.response.DeploymentId);
...
} else {
assert.equal('string', data.response.CreatedDate);
}
saveMockData('Apis', 'createDeployment', 'default', data);
done();
} catch (err) {
log.error(`Test Failure: ${err}`);
done(err);
}
});
} catch (error) {
log.error(`Adapter Exception: ${error}`);
done(error);
}
}).timeout(attemptTimeout);
});
Capture Mock Data from Adapter Log
You need to have the adapter in debug mode in order to capture these logs. Then you can use these logs for mock data for future testing by storing the response part of the log into the mock data file for the specific call.
CALL RETURN
– This is the full response prior to any JSON parsing.
ADAPTER LOGS
debug: Test-eai-connectorRest-makeRequest: CALL RETURN:
{"status":"success","code":201,"response":"{\n\"type\" :
\"oci/card\",\n\"id\" : 308,\n\"href\" :
\"oci/card/308\",\n\"transientAttributes\" : { },\n\"key\" :
{\n\"type\" : \"oci/cardKey\",\n\"keyValue\" : 308\n}\n}","redirects":0,"tripTime":"197ms"}
CALL RESPONSE
–This is the response or a partial response after JSON parsing.
ADAPTER LOGS
debug: Test-eai-restHandler-handleRestRequest: RESPONSE:
{"type":"oci/card","id":308,"href":"oci/card/308","transientAttributes"
:{},"key" :{"type":"oci/cardKey","keyValue":308}}
Was this article helpful?