Organizing the definitions
#
Files structureMocks server is very flexible and permissive with the files structure inside the mocks
folder. It will load any fixture or behavior defined in any .js
or .json
file at any folder level.
You can have your fixtures and behaviors definitions separated and organized in as many files and folders as you want.
#
FormatsAs read in the fixtures and behaviors chapters, fixtures and behaviors can be defined also using javascript. You can combine both formats, adding some of your definitions using javascript, while other are defined as plain json
objects.
#
Definitions in json filesYou can define your fixtures or behaviors in json format, defining one per file, or in an array containing multiple of them, or even all of them (fixtures and behaviors can also be defined in the same file)
#
One definition per file#
Multiple definitions per file#
Javascript exportation formatsJavascript files containing fixtures or behaviors can export them as:
#
ObjectYou can use an object to export your definitions. The Mock Server will search at the first level of exported objects and will load any fixture or behavior defined in it.
#
ArrayThe server accepts exports as arrays, and will load any fixture or behavior defined in it.
#
Single exportYou can also define one behavior or fixture per file, and export it directly:
#
Good practices#
Files structureAs a good practice, for a better maintainability, we recommend to use json
for the definitions while it is possible (for complex or programmatic definitions you'll prefer to use javascript), and maintain all your behavior definitions inside a behaviors.json
file in the root of the mocks
folder.
To organize fixtures, a good approach can be to create a folder for each api "domain", containing a different json
file with all fixtures of a same entity:
#
Descriptive idsWe strongly encourage to assign very descriptive ids to the "fixtures" and "behaviors", as they will be used afterwards in the CLI, the Api, and all other possible Mocks Server interaction methods.
A good pattern for assigning an id to a fixture can be [method]-[entity]-[short-description]
, as in read-user-success
, read-user-error
, or read-user-with-long-name
.
For assigning id to behaviors, we recommend to maintain a base behavior named as standard
, base
, or default
. The rest of behaviors should extend from it (at least indirectly), and their ids should be a short description of the behavior itself, for example: