【发布时间】:2019-08-11 02:50:15
【问题描述】:
我正在尝试模拟一个名为 callApi 的函数。我使用 jest.fn(),但我收到错误消息:
function callApi(method: string, url: string, path: string, data?: any): Promise> 无法分配给“callApi”,因为它是只读属性。ts(2540)
我已尝试按照以下示例进行操作 jest examples
我的代码有什么问题?为什么我会收到错误消息。
callApi 的一部分是
从“axios”导入axios;
export function callApi(
method: string,
url: string,
path: string,
data?: any
) {
switch (method) {
测试如下:
import {runSaga} from 'redux-saga';
import * as api from '../Utilities/api'
import { getPaymentsError, getPaymentsSuccess, IPaymentsAction } from './actions';
import handleFetch from './sagas'
test('should test fetch payments success',async() =>{
const dispatchedActions = [{}];
const mockedPayments = [{
details: {
amount: "10000",
date: new Date(),
id: 5
},
id: 5,
month: "Feb 2003",
userID: 3
}];
api.callApi = jest.fn(() => Promise.resolve(mockedPayments));<----------error here
const fakeStore = {
dispatch:(action:IPaymentsAction) =>dispatchedActions.push(action)
}
await runSaga(fakeStore,handleFetch).done;
expect(api.callApi.mock.calls.length).toBe(1);
expect(dispatchedActions).toContainEqual(getPaymentsSuccess(mockedPayments));
})
【问题讨论】:
标签: jestjs