ctx.mockImplementationOnce(value[, onAccess])


  • value <any> 用于模拟实现的值,对应于 onAccess 指定的调用编号。

    \value <any> The value to be used as the mock's implementation for the invocation number specified by onAccess.

  • onAccess <integer> 将使用 value 的调用编号。如果指定的调用已经发生,则抛出异常。默认值:下一次调用的次数。

    \onAccess <integer> The invocation number that will use value. If the specified invocation has already occurred then an exception is thrown. Default: The number of the next invocation.

此函数用于更改单个调用的现有模拟的行为。一旦调用 onAccess 发生,模拟将恢复到没有调用 mockImplementationOnce() 时它会使用的任何行为。

\This function is used to change the behavior of an existing mock for a single invocation. Once invocation onAccess has occurred, the mock will revert to whatever behavior it would have used had mockImplementationOnce() not been called.

以下示例使用 t.mock.property() 创建一个模拟函数,调用模拟属性,在下次调用时将模拟实现更改为不同的值,然后恢复其先前的行为。

\The following example creates a mock function using t.mock.property(), calls the mock property, changes the mock implementation to a different value for the next invocation, and then resumes its previous behavior.

test('changes a mock behavior once', (t) => {
 const obj = { foo: 1 };
 const prop = t.mock.property(obj, 'foo', 5);
 assert.strictEqual(obj.foo, 5);
 prop.mock.mockImplementationOnce(25);
 assert.strictEqual(obj.foo, 25);
 assert.strictEqual(obj.foo, 5);
}); 

AltStyle によって変換されたページ (->オリジナル) /