-
Notifications
You must be signed in to change notification settings - Fork 0
Cookbook: mock
Eugene Lazutkin edited this page Nov 26, 2019
·
8 revisions
In order to use io.mock
make sure that it is enabled:
// activate the package
io.mock.attach();
See How to include for more details. See mock for the full documentation.
io.mock('/url', function () {
return 42;
});
// ...
io.get('/url').then(function (value) {
console.log('Got:', value);
});
// prints: Got: 42
io.mock('/a', function () {
return io.get('/b');
});
io.mock('/url', function () {
// return something
});
// ...
io.get('/url').then(function (value) {
// gets our mocked value
});
io.get('/url/a').then(function (value) {
// unmocked request
});
io.mock('/url*', function () {
// return something
});
// ...
io.get('/url').then(function (value) {
// gets our mocked value
});
io.get('/url/a').then(function (value) {
// gets our mocked value
});
io.mock(/^\/url/, function () {
// return something
});
// ...
io.get('/url').then(function (value) {
// gets our mocked value
});
io.get('/url/a').then(function (value) {
// gets our mocked value
});
const matcher = options =>
options.method !== 'GET' &&
/\/url\//i.test(options.url);
io.mock(matcher, function () {
// return something
});
// ...
io.get('/url').then(function (value) {
// gets our mocked value
});
io.get('/url/a').then(function (value) {
// gets our mocked value
});
In this example, we will removed mocks set up earlier:
io.mock('/url');
io.mock('/url*');
io.mock(/^\/url/);
io.mock(matcher);
We use timeout of heya-async:
io.mock('/url', function () {
return timeout.resolve(500, io.Deferred).then(function () {
return 42;
});
});
io.mock('/url', function () {
return io.get('/a').then(function (value) {
return io.get('/b', {q: value.id});
});
});
io.mock('/url', function () {
return io.mock.makeXHR({
status: 500
});
});