温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/node/test/default/MockTracker/method
点击访问原文链接

MockTracker.method method | Node.js node:test module | Bun

MockTracker.method method | Node.js node:test module | BunBuildDocsReferenceGuidesBlogDiscord/node:test/default/MockTracker/methodMmethod

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/node:test/default/MockTracker/methodMmethod

method

test.default.MockTracker.method { const number = { value: 5, subtract(a) { return this.value - a; }, }; t.mock.method(number, 'subtract'); assert.strictEqual(number.subtract.mock.calls.length, 0); assert.strictEqual(number.subtract(3), 2); assert.strictEqual(number.subtract.mock.calls.length, 1); const call = number.subtract.mock.calls[0]; assert.deepStrictEqual(call.arguments, [3]); assert.strictEqual(call.result, 2); assert.strictEqual(call.error, undefined); assert.strictEqual(call.target, undefined); assert.strictEqual(call.this, number); }); ```" data-algolia-static="false" data-algolia-merged="false" data-type="Method">methodMockedObject extends object, MethodName extends string | number | symbol>(object: MockedObject,methodName: MethodName,options?: MockFunctionOptions): MockedObject[MethodName] extends Function ? Mockany[any]> : never;

This function is used to create a mock on an existing object method. The following example demonstrates how a mock is created on an existing object method.

test('spies on an object method', (t) => {
const number = {
value: 5,
subtract(a) {
return this.value - a;
},
};

t.mock.method(number, 'subtract');
assert.strictEqual(number.subtract.mock.calls.length, 0);
assert.strictEqual(number.subtract(3), 2);
assert.strictEqual(number.subtract.mock.calls.length, 1);

const call = number.subtract.mock.calls[0];

assert.deepStrictEqual(call.arguments, [3]);
assert.strictEqual(call.result, 2);
assert.strictEqual(call.error, undefined);
assert.strictEqual(call.target, undefined);
assert.strictEqual(call.this, number);
});
@param object

The object whose method is being mocked.

@param methodName

The identifier of the method on object to mock. If object[methodName] is not a function, an error is thrown.

@param options

Optional configuration options for the mock method.

@returns

The mocked method. The mocked method contains a special mock property, which is an instance of MockFunctionContext, and can be used for inspecting and changing the behavior of the mocked method.

methodMockedObject extends object, MethodName extends string | number | symbol, Implementation extends Function>(object: MockedObject,methodName: MethodName,implementation: Implementation,options?: MockFunctionOptions): MockedObject[MethodName] extends Function ? MockImplementation | any[any]> : never;methodMockedObject extends object>(object: MockedObject,methodName: keyof MockedObject,options: MockMethodOptions): MockFunction>;methodMockedObject extends object>(object: MockedObject,methodName: keyof MockedObject,implementation: Function,options: MockMethodOptions): MockFunction>;

Resources

ReferenceDocsGuidesDiscordMerch StoreGitHubBlog 

Toolkit

RuntimePackage managerTest runnerBundlerPackage runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicense

Baked with ❤️ in San Francisco

We're hiring →

MockTracker.method method | Node.js node:test module | Bun,AI智能索引,全网链接索引,智能导航,网页索引

    API documentation for method node:test.default.MockTracker.method | Bun