Version: 1.3.0

Behaviors

Definition#

Each behavior consists in a set of "fixtures", which are server responses for specific uris.

The Mocks Server can handle multiple behaviors, so you can change the API responses at your convenience just in a second.

Creating a behavior#

For creating a Behavior, you have to use the mocks-server Behavior class, providing an array of "fixtures" to it:

Read the "fixtures" code example to see how fixtures were defined first.

const { Behavior } = require("@mocks-server/main");
const { getUsers, getUser, updateUser } = require("./fixtures/users");
const standard = new Behavior([
getUsers,
getUser,
updateUser
]);
module.exports = {
standard
};

Save this file in the behaviors folder that you defined for your mocks-server when started.

Now, when loaded, the server will have available an "standard" behavior, which contains three fixtures.

Extending behaviors#

Behaviors are extensibles, so, you can have an "standard" behavior, which defines the default behavior of the mocks server and responses for all api uris, and modify this behavior creating new ones that change only responses for certain "uris". All extended behaviors are extensible as well.

You can add another one behavior extending the first one and changing only the response for getUsers, for example:

const { Behavior } = require("@mocks-server/main");
const { getUsers, getUser, updateUser, updateUserError } = require("./fixtures/users");
const standard = new Behavior([
getUsers,
getUser,
updateUser
]);
const errorUpdatingUser = standard.extend([updateUserError]);
module.exports = {
standard,
errorUpdatingUser
};

Now, server will have available "standard" and errorUpdatingUser behaviors.

The errorUpdatingUser behavior will send a different response only for the /api/users/:id uri with PUT method (supposing that updateUser and errorUpdatingUser have the same value for the url property).