Skip to content
Eugene Lazutkin edited this page Nov 26, 2019 · 4 revisions

io.mock mocks I/O requests by calling a client-based function, which decides what to return as a value for the request. It is indispensable for rapid application development, and for testing of any kind.

Don't forget to consult the cookbook to see working snippets solving simple real-world problems.

Logic

If options object has a Boolean property mock, it tells the track service to process or ignore a request. Otherwise, a default algorithm is used described in Service documentation.

io.mock(url, callback)

This procedure associates a callback with a URL.

The first parameter url can be:

  • a string: if a URL ends with '*', it assumes to be a prefix (without a star character), otherwise, an exact match is assumed.
  • a regular expression: it runs with test() method. If it matches and test() returns true, its callback will be called and no more attempts to match a request will be made.
  • a function: it is called with the same parameters as Service. If it returns a truthy value, its callback will be called and no more attempts to match a request will be made.

The second parameter `callback can be:

  • a function that takes the same parameters as Service. It can return a full-blown XHR object — useful to simulate headers, and server errors — see io.mock.makeXHR() below for details, or a promise, or an actual value.
  • a falsy value. In this case, the corresponding mock will be unregistered.

See the Cookbook: mock for ideas.

Exposed properties

io.mock.makeXHR(xhr)

This function returns a simulated XHR object. It takes an object. Following properties are recognized:

  • status — a server status. Default: 200.
  • statusText — a server status text as a string. Default: 'OK'.
  • responseType — an expected response type as a string. Default: ''.
  • responseText — a payload data as a string. Default: ''.
  • headers — a list of server headers as a string. Default: ''.

See XMLHttpRequest for details on each property.

io.mock.exact

A dictionary object, which keys are exact URL strings to match.

io.mock.prefix

A sorted array of objects with two properties:

  • url — a URL prefix to match as a string.
  • callback — a function to call, when we have a match.

Items are sorted in the reversed order or prefix length. Prefixes of the same length are sorted alphabetically.

io.mock.regexp

An array of objects with two properties:

  • url — a regular expression to match a URL.
  • callback — a function to call, when we have a match.

Items are stored on a first-come-first-served basis. The first matching entry always wins. Attempts to add the same regular expression several times will replace the current callback.

io.mock.match

An array of objects with two properties:

  • url — a function to match a URL.
  • callback — a function to call, when we have a match.

Items are stored on a first-come-first-served basis. The first matching entry always wins. Attempts to add the same regular expression several times will replace the current callback.

Standard service properties

See Service documentation for more details.

io.mock redefines a property theDefault setting it to true. It makes all I/O requests suitable for the service unless expressly prohibited by setting options.mock to false.

Clone this wiki locally