ctx.mockImplementation(implementation)
implementation<Function> | <AsyncFunction> 将作为模拟新实现的函数。
此函数用于更改现有模拟的行为。
【This function is used to change the behavior of an existing mock.】
下面的示例使用 t.mock.fn() 创建了一个模拟函数,调用该模拟函数,然后将模拟实现更改为另一个函数。
【The following example creates a mock function using t.mock.fn(), calls the
mock function, and then changes the mock implementation to a different function.】
test('changes a mock behavior', (t) => {
let cnt = 0;
function addOne() {
cnt++;
return cnt;
}
function addTwo() {
cnt += 2;
return cnt;
}
const fn = t.mock.fn(addOne);
assert.strictEqual(fn(), 1);
fn.mock.mockImplementation(addTwo);
assert.strictEqual(fn(), 3);
assert.strictEqual(fn(), 5);
});