Mocking service responses during development and testing
Key Takeaway
When a service is mocked, mockFull replaces the entire call, so createRequest and parseResponse don't run. With only mockCall, just the execute phase is replaced, so createRequest and parseResponse still run against the canned response.
B2C Commerce lets you replace a real web service call with a simulated response. You can then build and test integration code without calling the live service. You write the fake response in a mock callback on the service, such as mockCall. Then you turn mocking on in one of two ways: set the service's "Service Mode" to "Mocked" in Business Manager, or force it in code with setMock(). Your choice between mockCall and mockFull decides which of your other callbacks still run.
The service and its callbacks
You create a service instance with LocalServiceRegistry.createService(). The method takes two arguments. The first is the ID of a service configured in Business Manager. The second is an object of callback handlers that you implement. An HTTP or FTP service instance typically implements three callbacks: createRequest, parseResponse and, for testing, mockCall.
A call that isn't mocked includes these phases, in order:
createRequestuses the arguments passed tocall()to configure the actual service request.- The execute phase performs the actual request.
parseResponseconverts the result into the object thatcall()returns.
var service = LocalServiceRegistry.createService('example.giftcard.balance', {
createRequest: function (svc, args) {
// configure the request from the arguments passed to call()
},
parseResponse: function (svc, response) {
// convert the response into the object that call() returns
},
mockCall: function (svc, requestObj) {
return { statusCode: 200, statusMessage: "Success", text: "MOCK RESPONSE (" + svc.url + ")" };
}
});The first argument, 'example.giftcard.balance', stands for the ID of a service configured in Business Manager. Here mockCall returns a stand-in HTTP response with a status code, a status message and body text. An FTP mockCall can instead return an array of file entries, each with a name and a timestamp.