Cannot use the same scenario but different response
Closed this issue · 2 comments
In case API is the same but different in the body in a request.
Is this possible to use ng-Apimock?
Example:
API /api/everything
body request: { "source" : "customer", "id" : "123" }
response: mocks/data/customer.json
API /api/everything
body request: { "source" : "order", "id" : "O0001" }
response: mocks/data/order_o0001.json
Yes, by using select selectScenario
you can change what data is returned from the API.
The example mock below will return different data after selectScenario
is used to call either welcome-title
or real-welcome-title
. You will need to run your test inside the promise returned by selectScenario
after.
ngApimock.selectScenario('welcome', 'welcome-title').then(() => {
// Mock will return welcome-title data
});
ngApimock.selectScenario('welcome', 'real-welcome-title').then(() => {
// Mock will return real-welcome-title data
});
Example Mock:
{
"name": "welcome",
"isArray": "false",
"request": {
"url": "api/v1/welcome",
"method": "GET"
},
"responses": {
"welcome-title": {
"data": { "message": "This is the wrong title, yet is default." },
"default": true
},
"real-welcome-title": {
"data": { "message": "This is the title I want, but selectScenario is not setting it." },
"default": false
}
}
}
@JongchanaP I have rewritten the apimock to https://github.com/ng-apimock/core but haven't updated this github repo yet.
In the new version you can match request url, method, headers and payload.
This should do what you want. (https://github.com/ng-apimock/core#how-to-write-mocks)