Javascript 为什么getting无法读取属性';然后';post调用的未定义in jest测试的定义?
我需要一些开玩笑测试的帮助。我收到错误Javascript 为什么getting无法读取属性';然后';post调用的未定义in jest测试的定义?,javascript,reactjs,redux,jestjs,Javascript,Reactjs,Redux,Jestjs,我需要一些开玩笑测试的帮助。我收到错误TypeError:无法读取测试中未定义的属性“then” 行动中的签名功能创建者进行后期调用 import {SIGNING_IN, SIGNED_IN, SIGNING_FAILED, SIGNED_OUT} from "../actionTypes"; import {makePostCall} from "../../api/apiCalls"; export function signIn(data){ return dispatch =
TypeError:无法读取测试中未定义的属性“then”
行动中的签名功能创建者进行后期调用
import {SIGNING_IN, SIGNED_IN, SIGNING_FAILED, SIGNED_OUT} from "../actionTypes";
import {makePostCall} from "../../api/apiCalls";
export function signIn(data){
return dispatch => {
dispatch({type: SIGNING_IN});
makePostCall('http://localhost:8000/api/v1/signin', data)
.then(response => {
const auth = {token: response.token, userId: response.id};
localStorage.setItem('auth', JSON.stringify(auth));
dispatch({type: SIGNED_IN, message: 'You signed in successfully.'});
})
.catch(error => {
console.log('error: ', error);
dispatch({type: SIGNING_FAILED, message: 'Email or Password incorrect. Please try again!'});
});
}
}
export function makePostCall(url, data){
return axios({method: 'post', url, data})
.then(response => response.data)
}
这是在上述调用中调用的调用后函数
import {SIGNING_IN, SIGNED_IN, SIGNING_FAILED, SIGNED_OUT} from "../actionTypes";
import {makePostCall} from "../../api/apiCalls";
export function signIn(data){
return dispatch => {
dispatch({type: SIGNING_IN});
makePostCall('http://localhost:8000/api/v1/signin', data)
.then(response => {
const auth = {token: response.token, userId: response.id};
localStorage.setItem('auth', JSON.stringify(auth));
dispatch({type: SIGNED_IN, message: 'You signed in successfully.'});
})
.catch(error => {
console.log('error: ', error);
dispatch({type: SIGNING_FAILED, message: 'Email or Password incorrect. Please try again!'});
});
}
}
export function makePostCall(url, data){
return axios({method: 'post', url, data})
.then(response => response.data)
}
签名方法测试
jest.mock('../../../src/api/apiCalls');
describe('authenticationActionCreators', () => {
describe('signIn', () => {
let dispatch;
beforeEach(() => {
jest.clearAllMocks();
const authResponse = getAuthResponse();
makePostCall.mockReturnValue(Promise.resolve(authResponse));
dispatch = jest.fn();
});
test('should make post call with correct URL and data', () => {
const data = {email: 'user@user.com', password: 'password'};
return signIn(data)(dispatch).then(() => {
expect(makePostCall).toHaveBeenCalledWith('http://localhost:8000/api/v1/signin', {
email: 'user@user.com',
password: 'password'
})
})
});
});
每当我运行测试时,我在返回签名(数据)(调度)行上得到错误。然后(()=>{我做错了。我更改了它,它工作了
test('should make post call with correct URL and data', () => {
const data = {email: 'user@user.com', password: 'password'};
signIn(data)(dispatch);
expect(makePostCall).toHaveBeenCalledWith('http://localhost:8000/api/v1/signin', {
email: 'user@user.com',
password: 'password'
})
});
sign(…)(…)
不返回任何内容。您忘记了return
在makePostCall(…)之前http://...“,data)
@str你是说?登录(数据)(调度)。然后(()=>{返回expect(makePostCall)。以(“”,{电子邮件:'user@user.com,password:'password'})它仍然会给出相同的错误。否。我的意思是你应该直接在makepostall('s)前面添加return
http://...,数据)
(即返回makePostCall('http://...,数据)
)。