snion中文文档 Fakes

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.spysinon.stub 方法不同,sinon.fakeAPI 仅关注如何创建 fake,而与将fake插入被测系统无关。
要将fake插入被测系统,可以使用这些 sinon.replace\*方法。

创建一个 fake

创建一个有或没有行为的fake Function, 创建的函数都将具有与 sinon.spy 相同的 API。

  • Creating a fake without behavior
1
2
3
4
5
6
7
8
// create a basic fake, with no behavior
var fake = sinon.fake()

fake()
// undefined

fake.callCount
// 1
  • Creating a fake with custom behaviour
1
2
3
4
5
// create a fake that returns the text "foo"
var fake = sinon.fake.returns('foo')

fake()
// foo

Fakes 的行为

行为一旦创造,就无法改变。

sinon.fake.returns(value);

创建返回值参数的 fake。

1
2
3
4
var fake = sinon.fake.returns('apple pie')

fake()
// apple pie

sinon.fake.throws(value);

创建一个 fake,该 fake 将使用提供的值作为 message 属性抛出 Error。

如果将 Error 作为 value 参数传递,则将作为抛出的值。 如果传递了任何其他值,则该值将用于引发 Error 的 message 属性。

1
2
3
4
var fake = sinon.fake.throws(new Error('not apple pie'))

fake()
// 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
2
3
4
5
6
7
8
var fake = sinon.fake.yields(null, 'file content')
sinon.replace(fs, 'readFile', fake)
fs.readFile('somefile', (err, data) => {
console.log(data)
})
console.log('end of this event loop')
// file content
// end of this event loop

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
2
3
4
5
6
7
8
var fakeAsync = sinon.fake.yieldsAsync(null, 'file content')
sinon.replace(fs, 'readFile', fakeAsync)
fs.readFile('somefile', (err, data) => {
console.log(data)
})
console.log('end of this event loop')
// end of this event loop
// 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
2
3
4
5
6
7
8
9
var f = sinon.fake()
var cb1 = function () {}
var cb2 = function () {}

f(1, 2, 3, cb1)
f(1, 2, 3, cb2)

f.callback === cb2
// true

The same convenience has been added to spy calls:

1
2
3
4
5
f.getCall(1).callback === cb2
// true
//
f.lastCall.callback === cb2
// true

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
2
3
4
5
6
7
8
9
var f = sinon.fake()
var date1 = new Date()
var date2 = new Date()

f(date1, 1, 2)
f(date2, 1, 2)

f.firstArg === date2
// true

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
2
3
4
5
6
7
8
9
var f = sinon.fake()
var date1 = new Date()
var date2 = new Date()

f(1, 2, date1)
f(1, 2, date2)

f.lastArg === date2
// true

The same convenience has been added to spy calls:

1
2
3
4
5
6
7
f.getCall(0).lastArg === date1
// true
f.getCall(1).lastArg === date2
// true

f.lastCall.lastArg === date2
// true

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
2
3
4
5
6
var fake = sinon.fake.returns('42')

sinon.replace(console, 'log', fake)

console.log('apple pie')
// 42

When you want to restore the replaced properties, call the sinon.restore method.

1
2
// restores all replaced properties set by sinon methods (replace, spy, stub)
sinon.restore()
Author: liuarui
Link: https://liuarui.github.io/2021/04/26/框架/snion中文文档/Fakes/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.