Jestjs 无法期望使用不同参数调用'TohavenCalledWith'

Jestjs 无法期望使用不同参数调用'TohavenCalledWith',jestjs,Jestjs,我模拟了MongoDB实例updateOne。在这段代码中,我发现了调用updateOne的UpdateByID。对于成功的案例,writeById方法调用UpdateByID两次。所以我希望已经被调用了两次,使用不同的参数。但是当我运行我的代码时,它两次期望相同的值。有人能告诉我我的代码出了什么问题吗 测试用例文件 const mongoDBInstance = new MongoDb( new MongoClient('testClient'), {

我模拟了MongoDB实例
updateOne
。在这段代码中,我发现了调用updateOne的
UpdateByID
。对于成功的案例,
writeById
方法调用
UpdateByID
两次。所以我希望
已经被调用了两次,使用不同的参数。但是当我运行我的代码时,它两次期望相同的值。有人能告诉我我的代码出了什么问题吗

测试用例文件

const mongoDBInstance = new MongoDb(
        new MongoClient('testClient'),
        {
            updateOne: jest.fn(),

        } as unknown as Collection
    );

it('should perform writeById on mongodb if parameters are valid.', async () => {
            // Preparing
            const mockUpdateByIdReturn = {
                result: {ok: 1, n: 1, nModified: 1},
                connection: 0,
                matchedCount: 1,
                modifiedCount: 1,
                upsertedCount: 0,
                upsertedId: {_id: id}
            };
            const validDocument = { string_field : 'sample'};
            // Executing
            const spyUpdateByID = jest.spyOn(MongoDb.prototype, 'updateById')
                .mockReturnValueOnce(Promise.resolve(mockUpdateByIdReturn)) // updateById -> updateOne - document
                .mockReturnValueOnce(Promise.resolve({} as any)); // updateById -> updateOne - createAutoFieldsForUpdateRequest
            const result = await model.writeById(id, validDocument, EndpointType.UPDATE);
            // Verifying
            expect(result).toEqual({
                type: 'success',
                message: 'ModuleA Updated Success Constant.',
                statusCode: 204
            });
            expect(spyUpdateByID).toHaveBeenNthCalledWith(1, id, validDocument);
            expect(spyUpdateByID).toHaveBeenNthCalledWith(2, id, {update_auto_field: new Date()});
            // Clearing
            spyUpdateByID.mockClear();
        });
模型文件(在此方法中,updateAutoFields还调用
updateById

writeById=async(id:string | ObjectId,document:Partial):
承诺=>{
让mongodb:mongodb;
//更新数据
mongodb=wait mongodb.connect(this.schemaConstants['collectionName']);
const dbResponse:UpdateWriteOpResult=await mongodb.updateById(id,modelDocument);
mongodb.close();
//更新自动字段

等待ModelUtility.updateAutoFields(id,(

我遇到了同样的问题,就我所知,这是显示内容中的一个错误。我断言的参数中有一个错误。解决了这个问题后,测试通过了。要查看传入的内容,请调试spyUpdateById.mock.calls。我遇到了同样的问题,就我所知,这是显示内容中的一个错误。我断言的参数中有一个错误参数。一旦我解决了这个问题,测试就通过了。要查看传入的内容,请调试spyUpdateById.mock.calls。
  writeById = async (id: string | ObjectId, document: Partial<ModelType>):
        Promise<ValidationError | ResponseErrorMessageType | ResponseSuccessMessageType> => {
        let mongodb: MongoDb<ModelType>;
        // Update data
        mongodb = await MongoDb.connect(<string>this.schemaConstants['collectionName']);
        const dbResponse: UpdateWriteOpResult = await mongodb.updateById(id, <ModelType>modelDocument);
        mongodb.close();
        // Update auto fields
        await ModelUtility.updateAutoFields(id, (<AbstractBuilder<ModelType>><unknown>modelDocument));
        return <ResponseSuccessMessageType>this.successConstants.updatedSuccessfully;
    };