javascript - 开玩笑的单元测试“期望模拟函数最后被调用,但没有被调用”
问题描述
我是 Jest 单元测试的新手,我尝试编写一个测试,但它一直说'expected mock function to have been called last with, but it was not called'
不确定问题出在哪里。下面是我编写了一个 API 调用的 APIClient.js 文件,在测试文件中我模拟了数据,它记录了
Expected mock function to have been last called with:
["/.../test1/...", {"argu4": "test4", "argu5": "test5", "name": [{"argu3": "test3"}, "argu2": "test2"]}, {"headers": {"Content-Type": "application/json", "guest": false, "locale": 'US'}}}]
but it was not called.
APIClient.js
const { getLeapFrogHeaders } = require('...');
async myFunc(argu1, argu2, argu3, argu4, argu5) {
const header = getLeapFrogHeaders();
const requestBody = {
name: [{ argu2, argu3}],
argu4,
argu5
}
const response = await this.connector.patch(`/.../${argu1}/...`, requestBody, { headers });
return response.data;
}
APIClient.test.js
import APIClient from './';
import MockConnector from './';
const mockConnector = new MockConnector({});
const APIClient = new APIClient(mockConnector);
describe('APIClient test', () => {
const headers = {
'Content-Type' : 'application/json',
guest: false,
locale: 'US'
};
it('successfully create API', () =>{
const requestBody = {
name: [{ argu2: 'test2', argu3: 'test3'}],
argu4: 'test4',
argu5: 'test5'
};
const argu1 = 'test1';
const spy = jest.spyOn(mockConnector, 'post');
getLeapFrogHeaders.mockImplementation(() => headers);
APIClient.myFunc(argu1, 'test2', 'test3', 'test4', 'test5');
expect(spy).toHaveBeenLastCalledWith(`/.../${argu1}/...`, requestBody, { headers })
})
})
解决方案
推荐阅读
- sql - 如何使用下一个日期列值来计算当前列的增量
- angular - Angular Schematics:trustedSubscriber._addParentTeardownLogic
- php - 问题比较php中的值
- apache-spark - 无法从主机的传输池中获取传输
- c++ - VS 2012 的嵌套命名空间混淆
- javascript - 从javascript加载动态视频源?
- java - 从 Quartz 中的作业类名称获取作业密钥
- angular - Angular 6 / 在 tsconfig.lib.json 中声明库的路径
- java - 将参数设置为 rawQuery 时出现 SQLiteException
- javascript - 当页面内容通过 jQuery ajax 调用响应在 DOM 上呈现时,在视图上呈现脚本部分