Fakes
介绍
-
fake
从 v5 版本开始可在 Sinon 中使用。 - fake 拥有创建具有设置默认行为的
fake Function
功能。 - 使用具有与
sinon.stub
中相同的 API 的Functions
来设/Users/lm/Desktop/note/snion 中文文档/Fakes.md 置行为 - 创建的具有或不具有行为的
fake Function
具有与(sinon.spy
)spies
相同的 API。
在 Sinon
中,fake
是一个 Function
它记录其所有调用的参数,返回值,this 的值和引发的异常(如果有)。fake
是不变的:一旦创建,行为就不会改变。
与 sinon.spy
和 sinon.stub
方法不同,sinon.fakeAPI
仅关注如何创建 fake,而与将fake
插入被测系统无关。
要将fake
插入被测系统,可以使用这些 sinon.replace\*
方法。
创建一个 fake
创建一个有或没有行为的fake Function
, 创建的函数都将具有与 sinon.spy
相同的 API。
- Creating a fake without behavior
1 | // create a basic fake, with no behavior |
- Creating a fake with custom behaviour
1 | // create a fake that returns the text "foo" |
Fakes 的行为
行为一旦创造,就无法改变。
sinon.fake.returns(value);
创建返回值参数的 fake。
1 | var fake = sinon.fake.returns('apple pie') |
sinon.fake.throws(value);
创建一个 fake,该 fake 将使用提供的值作为 message 属性抛出 Error。
如果将 Error 作为 value 参数传递,则将作为抛出的值。 如果传递了任何其他值,则该值将用于引发 Error 的 message 属性。
1 | var fake = sinon.fake.throws(new Error('not apple pie')) |
sinon.fake.resolves(value);
Creates a fake that returns a resolved Promise for the passed value.
sinon.fake.rejects(value);
Creates a fake that returns a rejected Promise for the passed value.
如果错误被作为值参数传递,那么这将是 promise 的价值。
如果传递了其他任何值,则该值将用于 promise 返回的 Error 的 message 属性。
sinon.fake.yields([value1, …, valueN]);
sinon.fake.yields takes some values, and returns a function that when being called, expects the last argument to be a callback and invokes that callback with the same previously given values. The returned function is normally used to fake a service function that takes a callback as the last argument.
In code example below, the ‘readFile’ function of the ‘fs’ module is replaced with a fake function created by sinon.fake.yields. When the fake function is called, it always calls the last argument it received, which is expected to be a callback, with the values that the yields function previously took.
1 | var fake = sinon.fake.yields(null, 'file content') |
sinon.fake.yieldsAsync([value1, …, valueN]);
Similar to yields, yieldsAsync also returns a function that when invoked, the function expects the last argument to be a callback and invokes that callback with the same previously given values. However, the returned function invokes that callback asynchronously rather than immediately, i.e. in the next event loop.
Compare the output of the code example below with the output of the code example above for yields to see the difference.
1 | var fakeAsync = sinon.fake.yieldsAsync(null, 'file content') |
sinon.fake(func);
Wraps an existing Function to record all interactions, while leaving it up to the func to provide the behavior.
The created fake Function has the same API as a sinon.spy.
This is useful when complex behavior not covered by the sinon.fake.* methods is required or when wrapping an existing function or method.
Instance properties
The instance properties are the same as a sinon.spy.
f.callback
This property is a convenience to get a reference to the last callback passed in the last to the fake.
1 | var f = sinon.fake() |
The same convenience has been added to spy calls:
1 | f.getCall(1).callback === cb2 |
f.firstArg
This property is a convenient way to get a reference to the first argument passed in the last call to the fake.
1 | var f = sinon.fake() |
f.lastArg
This property is a convenient way to get a reference to the last argument passed in the last call to the fake.
1 | var f = sinon.fake() |
The same convenience has been added to spy calls:
1 | f.getCall(0).lastArg === date1 |
Adding the fake to the system under test
Unlike sinon.spy and sinon.stub, sinon.fake only knows about creating fakes, not about replacing properties in the system under test.
To replace a property, you can use the sinon.replace method.
1 | var fake = sinon.fake.returns('42') |
When you want to restore the replaced properties, call the sinon.restore method.
1 | // restores all replaced properties set by sinon methods (replace, spy, stub) |