-
Notifications
You must be signed in to change notification settings - Fork 0
mock
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.
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.
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()returnstrue, itscallbackwill 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
callbackwill 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.
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.
A dictionary object, which keys are exact URL strings to match.
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.
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.
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.
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.