mdasberg / ng-apimock

Node plugin that provides the ability to use scenario based api mocking: for local development for protractor testing
MIT License
99 stars 26 forks source link

Cannot use the same scenario but different response #67

Closed JongchanaP closed 5 years ago

JongchanaP commented 5 years ago

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

Local9 commented 5 years ago

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
    }
  }
}
mdasberg commented 5 years ago

@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)