我有一个api服务,在这里我有不同的方法来调用API。我已经成功测试了所有GET请求,但是在测试POST请求时遇到了麻烦。
这是方法:
export default class ApiService {
static makeApiCall = <T>(
url: string,
oneCb: <T>(d: Data) => T,
secondCb: (d: T) => void,
errorCb?: (a: ErrorModel) => void,
method = 'get',
data = {},
): Promise<void> => {
const config: AxiosRequestConfig = {};
if (method === 'post') {
config.headers = header;
return ApiClient.post(url, data, config)
.then(res => callback(normalizeCallback(res.data))).catch(error => someHandler(error));
} else {
return ApiClient.get(url)
.then(res => callback(normalizeCallback(res.data))).catch(error => someHandler(error));
}
};
// ONLY ONE POST METHOD TO MAKE IT MORE CLEAR
static someArchiveMethod = (
callback: (a: SuccessModel) => void,
errorCallback: (error: ErrorModel) => void,
cardId: string
): Promise<void> => {
return ApiService.makeApiCall<SuccessfulResponse>(
'appreciationCard/archive',
Normalizer.successfulResponse,
callback,
errorCallback,
'post',
{ cardId }
);
};
// HERE BELOW THE GET METHODS
static getPeople = (cb: (a: PeopleModel[]) => void, page?: number, limit?: number): Promise<void> => {
const queryDetails = { page, limit };
return ApiService.makeApiCall<PeopleModel[]>(
`people?${toQueryString(queryDetails)}`,
Normalizer.normalizePeople,
callback
);
};
};
这就是我测试与GET相关的所有内容的方式:
describe('apiService', () => {
beforeAll(() => {
expect(ApiClient.defaults.headers.common.Authorization).toBe('Bearer test token');
// @ts-ignore
ApiClient.get.mockImplementation((url: string) => {
return Promise.resolve({ data: mockData });
});
});
it('should call api client method', () => {
ApiService.makeApiCall(
'testUrl',
data => data,
res => res,
err => err,
'get'
);
expect(ApiClient.get).toBeCalledTimes(1);
expect(ApiClient.get).toBeCalledWith('testUrl');
});
it('should call callbacks consequently', done => {
ApiService.makeApiCall('testUrl', firstCallback, secondCallback).then(() => {
expect(firstCallback).toBeCalledTimes(1);
expect(firstCallback).toBeCalledWith(mockData);
expect(secondCallback).toBeCalledTimes(1);
expect(secondCallback).toBeCalledWith(firstCallback(mockData));
done();
});
});
});
describe('api service error flow', () => {
beforeAll(() => {
// @ts-ignore
ApiClient.get.mockImplementation((url: string) => {
console.log('error result');
return Promise.reject(mockError);
});
});
it('should handle error', done => {
console.error = jest.fn();
const firstCallback = jest.fn((data: any) => data);
const secondCallback = jest.fn((data: any) => data);
ApiService.makeApiCall('testUrl', firstCallback, secondCallback).then(() => {
expect(firstCallback).toBeCalledTimes(0);
expect(secondCallback).toBeCalledTimes(0);
expect(console.error).toBeCalledTimes(1);
expect(console.error).toBeCalledWith('ApiClient testUrl', mockError);
done();
});
});
});
describe('apiService methods', () => {
beforeAll(() => {
ApiClient.get.mockImplementation((url: string) => {
expect(ApiClient.defaults.headers.common.Authorization).toBe('Bearer test token');
return Promise.resolve({ data: mockData });
});
});
it('getPeople method call with one param', () => {
ApiService.getPeople(jest.fn(), 1, 1).then(() => {
expect(ApiClient.get).toBeCalledWith('people?page=1&limit=1');
});
});
})
我认为,只有通过改变所有的实例ApiClient.get
,以ApiClient.post
它的工作来测试POST请求。但是,当我尝试这样做时,它就是这么说的can not read mockImplementation of undefined
。我尝试更改测试中的方法以使用post
参数以覆盖参数,method = 'get'
但是我没有成功,但出现此错误
TypeError:apiClient_1.default.post不是函数
有什么想法吗?
嗨,@Tomas看这行->
—
反应
return ApiClient.post(url, data, config) .then(res => callback(normalizeCallback(res.data))).catch(error => someHandler(error));
并且当我尝试执行发布请求时,它可以正常工作。我的意思是,我喜欢17个帖子请求,它们必须按要求工作。那么为什么在测试中不起作用呢?
@Reacting请分享“发布”单元测试示例
—
Oron Ben-David
@ OronBen-David我在问题中提到我尝试与
—
反应
get
测试完全相同,但是我更改了所有实例get
并设置了它post
。
我了解,但是提到不起作用的代码会更加清楚
—
Oron Ben-David
ApiClient
是没有方法post
。