Skip to content
Permalink
Browse files
Add yields and yieldsTo to fake
  • Loading branch information
mroderick committed Jan 8, 2018
1 parent 8c1e221 commit 67d0bd88278499076fbd4b2c42b4b4f3eb518934
Showing 3 changed files with 95 additions and 0 deletions.
@@ -68,6 +68,29 @@ Creates a fake that returns a rejected `Promise` for the passed value.

If an `Error` is passed as the `value` argument, then that will be the value of the promise. If any other value is passed, then that will be used for the `message` property of the `Error` returned by the promise.

#### `sinon.fake.yields(callback[, value1, ..., valueN]);`

Creates a fake that calls the provided callback with the provided values.

```js
var fake = sinon.fake.yields(console.log, 'hello world');
fake();
// hello world
```

#### `sinon.fake.yieldsAsync(callback[, value1, ..., valueN]);`

Creates a fake that calls the provided callback asynchronously with the provided values.

```js
var fake = sinon.fake.yieldsAsync(console.log, 'hello world');
fake();
// hello world
```


### `sinon.fake(func);`

Wraps an existing `Function` to record all interactions, while leaving it up to the `func` to provide the behaviour.
@@ -1,6 +1,7 @@
"use strict";

var spy = require("./spy");
var nextTick = require("./util/core/next-tick");

function getError(value) {
return value instanceof Error ? value : new Error(value);
@@ -73,4 +74,40 @@ fake.rejects = function rejects(value) {
return wrapFunc(f);
};

function yieldInternal(async, callback, values) {
function f() {
if (async) {
nextTick(function () {
callback.apply(null, values);
});
} else {
callback.apply(null, values);
}
}

return wrapFunc(f);
}

fake.yields = function yields() {
var callback = Array.prototype.slice.call(arguments, 0, 1)[0];
var values = Array.prototype.slice.call(arguments, 1);

if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a Function");
}

return yieldInternal(false, callback, values);
};

fake.yieldsAsync = function yieldsAsync() {
var callback = Array.prototype.slice.call(arguments, 0, 1)[0];
var values = Array.prototype.slice.call(arguments, 1);

if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a Function");
}

return yieldInternal(true, callback, values);
};

module.exports = fake;
@@ -28,6 +28,7 @@ function verifyProxy(func, argument) {
});
}

function noop() {}

describe("fake", function () {
describe("module", function () {
@@ -151,4 +152,38 @@ describe("fake", function () {
});
});
});

describe(".yields", function () {
verifyProxy(fake.yields, noop, "42", "43");

it("should call the callback with the provided values", function () {
var callback = sinon.spy();
var myFake = fake.yields(callback, "one", "two", "three");

myFake();

sinon.assert.calledOnce(callback);
sinon.assert.calledWith(callback, "one", "two", "three");
});
});

describe(".yieldsAsync", function () {
verifyProxy(fake.yieldsAsync, noop, "42", "43");

it("should call the callback asynchronously with the provided values", function (done) {
var callback = sinon.spy();
var myFake = fake.yieldsAsync(callback, "one", "two", "three");

myFake();

sinon.assert.notCalled(callback);

setTimeout(function () {
sinon.assert.calledOnce(callback);
sinon.assert.calledWith(callback, "one", "two", "three");

done();
}, 0);
});
});
});

0 comments on commit 67d0bd8

Please sign in to comment.