Version: 1.4.0

Behaviors

Definition#

Each behavior consists in a set of "fixtures", which are handlers for specific requests.

The Mocks Server can handle multiple behaviors, so you can change the API responses at your convenience while the server is running.

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 mocks folder in the root of your project.

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 mock server and responses for all your 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 of the getUsers fixture, 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, the 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 and method properties).