Skip to content

Fill responses with fake data

Before you begin

Role
Editor or higher.
You need
  • Response templating switched on in the project's settings, which it is by default

random fills a response with realistic fake values, such as names, emails, cities and numbers, drawn fresh on every call. cachedRandom does the same but repeats one value wherever it appears in the response, so a name and the email made from it belong to the same person.

  1. On the project’s Mocks tab, select the mock’s name.

  2. In Response body, type {{random ' where a value belongs. The editor lists the providers, and after the dot the methods of the one you chose.

  3. Complete the body:

    {
    "id": "{{random 'Internet.uuid'}}",
    "name": "{{random 'Name.fullName'}}",
    "email": "{{random 'Internet.safeEmailAddress'}}",
    "city": "{{random 'Address.city' locale='de'}}",
    "age": {{random 'Number.numberBetween' min=18 max=90}}
    }
  4. Select Save mock.

Every call returns new values, such as:

{
"id": "1a4089cc-5c8e-4c4b-91ef-7add3623c5f8",
"name": "Pierre Larson",
"email": "jann.dooley@example.com",
"city": "Busseheim",
"age": 51
}

Internet.safeEmailAddress gives addresses at example.com and similar reserved domains, which can never reach a real inbox; Internet.emailAddress uses real free-mail domains.

Every provider and method, with its arguments, is listed in Fake data.

A method’s arguments are written as name=value after it, using the names the reference lists, such as min and max for Number.numberBetween. Numbers are written bare and text in quotes: {{random 'Lorem.sentence' wordCount=3}}.

The arguments must be exactly one of the lists the reference shows for the method. Leaving one out fails, and so does adding one the method does not take; the editor underlines the second as you type.

For a method that takes something other than text or a number, write the value as text and MockHub converts it: a value from a fixed set by its name, in any letter case; a moment or a time of day in ISO 8601; a list as values separated by commas. Fake data names each method’s argument types and the values of each set.

{{random 'Planet.mass' planetName='earth'}}
{{random 'TimeAndDate.between' from='2024-01-01T00:00:00Z' to='2024-01-02T00:00:00Z'}}
{{random 'Internet.slug' wordsOrNull='alpha,beta' glueOrNull='-'}}

A value the set does not have is reported with the ones it has:

Planet.mass: planetName takes one of JUPITER, MARS, VENUS, URANUS, MERCURY, NEPTUNE, EARTH, SATURN, PLUTO.

locale= works with every method and chooses the language and country of the value, such as locale='de', locale='pt_BR' or locale='pl'. A region of a listed language is accepted. A locale MockHub has no data for gives English values without an error, so the editor warns about it; the reference lists every locale.

cachedRandom takes the same provider, method and arguments, plus a key=. Within one response, the same key always gives the same value:

{
"firstName": "{{cachedRandom 'Name.firstName' key='first'}}",
"email": "{{lower (cachedRandom 'Name.firstName' key='first')}}@example.com"
}
  • key= is required, and must be quoted text. Without it the mock cannot render its response; see When a value cannot be generated.
  • The key alone decides. A second cachedRandom with the same key returns the first value even if it names another method.
  • The value lasts one response. The next call draws a new one.

each repeats part of the body, and random gives each item its own value:

{
"items": [
{{#each (range 1 3)}}
{"id": {{this}}, "name": "{{random 'Commerce.productName'}}"}{{#unless @last}},{{/unless}}
{{/each}}
]
}

Use random inside each, not cachedRandom: a cached value is the same for the whole response, so every item would repeat it. The editor warns when you do.

A provider, method or argument that does not exist fails the whole response. The caller gets a 500 Mock response did not render error in its place, and the project’s request log says why, never repeating a value taken from the request:

Number.numberBetween takes min and max.

The editor catches most of these as you type. Try it finds the rest and marks them; see Test a mock before saving and A mock’s response did not render.

  • Values are random on every call, and nothing makes them repeat from one call to the next. For a value that must stay the same, write it into the body.
  • Values are drawn independently: a city and a postcode in the same response do not belong together.
  • random here is MockHub’s own. The {{random 'Name.first_name'}} form found in examples written for other mocking tools does not exist: provider and method names are written as the reference lists them.
  • An expression directly followed by a } of your JSON, as in {"age": {{random 'Number.digit'}}}, fails. Keep a space or line break before the brace.

Report a problem with this page