Behaviors
#
DefinitionThe Mocks Server can handle multiple behaviors, so you can change the API responses at your convenience while the server is running.
Each behavior consists in a set of "fixtures", which are handlers for specific requests. When a behavior contains multiple fixtures that should handle the same request (same method and url), the last one in the array will have priority over the first one. This has to be taken into account when we are extending behaviors.
#
Defining behaviors using jsonBehaviors are defined with an object containing next properties:
id
:<String>
Used as reference for extending it, etc.from
:<String>
Optional. Behavior id from which this behavior will extend.fixtures
:<Array of String>
Fixtures ids.
#
Extending behaviorsBehaviors are extensibles, so, you can have an "standard" behavior, which defines the default behavior of the mock server and responses for all your api uris, and extend this behavior creating new ones that change only responses for certain uris. All extended behaviors are extensible as well.
#
Defining behaviors using javascriptFor creating a behavior using javascript, you have to use the mocks-server Behavior
class, providing an array of "fixtures" or fixtures ids to it.
new Behavior(fixtures, options)
fixtures
:<Array>
of fixtures or fixtures ids.options
:<Object>
id
:<String>
Id for the behavior.
Read the "fixtures" code example to see how fixtures were defined first.
#
Extending behaviors instancesBehaviors instances contain an extend
method, which can be used to create a new behavior extending from it using javascript.
You can add another one behavior extending the first one and changing only the response of the updateUser
fixture, for example:
Now, the server will have available "standard" and "update-user-error" behaviors.
The "update-user-error" behavior will send a different response only for the /api/users/:id
uri with PUT
method (supposing that updateUser
and updateUserError
fixtures have the same value for the url
and method
properties).