Javascript承诺在React酶测试中不返回模拟值
我试图测试一个React组件,该组件包含对api库的调用,因此返回一个承诺 api库如下所示:(utils/api.js) 我对它进行了如下模拟:(utils/\uuuuumocks\uuuuu/api.js) 我正在测试的功能是:Javascript承诺在React酶测试中不返回模拟值,javascript,reactjs,jestjs,es6-promise,enzyme,Javascript,Reactjs,Jestjs,Es6 Promise,Enzyme,我试图测试一个React组件,该组件包含对api库的调用,因此返回一个承诺 api库如下所示:(utils/api.js) 我对它进行了如下模拟:(utils/\uuuuumocks\uuuuu/api.js) 我正在测试的功能是: create() { api.createTrip(this.state.trip).then(response => { if (response.status === 201) { this.setState
create() {
api.createTrip(this.state.trip).then(response => {
if (response.status === 201) {
this.setState({trip: {}, errors: []});
this.props.onTripCreate(response.data);
} else if (response.status === 400) {
this.setState({errors: response.data})
}
});
}
测试是:
jest.mock('utils/api.js');
test('succesful trip create calls onTripCreate prop', () => {
const trip = {'name': faker.random.word()};
const spy = jest.fn();
const container = shallow(<TripCreateContainer onTripCreate={spy}/>);
container.setState({'trip': trip});
container.instance().create();
expect(spy).toHaveBeenCalledWith(trip);
expect(container.state('trip')).toEqual({});
expect(container.state('errors')).toEqual([]);
});
succesful trip create calls onTripCreate prop
expect(jest.fn()).toHaveBeenCalledWith(expected)
Expected mock function to have been called with:
[{"name": "copy"}]
But it was not called.
at Object.test (src/Trips/__tests__/Containers/TripCreateContainer.jsx:74:21)
at new Promise (<anonymous>)
at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
at <anonymous>
jest.mock('utils/api.js');
测试('successful trip create calls onticreate prop',()=>{
常量trip={'name':faker.random.word()};
const spy=jest.fn();
常量容器=浅();
setState({'trip':trip});
container.instance().create();
期望(间谍)与(旅行)一起被逮捕;
expect(container.state('trip')).toEqual({});
expect(container.state('errors')).toEqual([]);
});
我相信这应该是可行的,但测试的结果是:
jest.mock('utils/api.js');
test('succesful trip create calls onTripCreate prop', () => {
const trip = {'name': faker.random.word()};
const spy = jest.fn();
const container = shallow(<TripCreateContainer onTripCreate={spy}/>);
container.setState({'trip': trip});
container.instance().create();
expect(spy).toHaveBeenCalledWith(trip);
expect(container.state('trip')).toEqual({});
expect(container.state('errors')).toEqual([]);
});
succesful trip create calls onTripCreate prop
expect(jest.fn()).toHaveBeenCalledWith(expected)
Expected mock function to have been called with:
[{"name": "copy"}]
But it was not called.
at Object.test (src/Trips/__tests__/Containers/TripCreateContainer.jsx:74:21)
at new Promise (<anonymous>)
at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
at <anonymous>
成功的trip create调用OnTipCreate prop
expect(jest.fn())。与(expected)一起调用
预期已使用以下函数调用模拟函数:
[{“名称”:“副本”}]
但它没有被称为。
在Object.test(src/Trips/__-tests\u_/Containers/TripCreateContainer.jsx:74:21)
在新的承诺()
在Promise.resolve.then.el(node_modules/p-map/index.js:46:16)
在
我不知道如何修复此测试,如果有人能帮助我,我将不胜感激。很接近
然后
将回调排队等待执行。当当前同步代码完成并且事件循环捕获下一个排队的内容时,将执行回调
测试正在运行到完成,并在排队的回调之前失败,然后create()
中的有机会运行
给事件循环一个循环的机会,这样回调就有机会执行,这应该可以解决问题。这可以通过使您的测试函数异步并等待一个已解决的承诺来完成,您希望暂停测试并让任何排队回调执行:
jest.mock('utils/api.js');
test('succesful trip create calls onTripCreate prop', async () => {
const trip = {'name': faker.random.word()};
const spy = jest.fn();
const container = shallow(<TripCreateContainer onTripCreate={spy}/>);
container.setState({'trip': trip});
container.instance().create();
// Pause the synchronous test here and let any queued callbacks execute
await Promise.resolve();
expect(spy).toHaveBeenCalledWith(trip);
expect(container.state('trip')).toEqual({});
expect(container.state('errors')).toEqual([]);
});
jest.mock('utils/api.js');
test('successful trip create calls onticreate prop',async()=>{
常量trip={'name':faker.random.word()};
const spy=jest.fn();
常量容器=浅();
setState({'trip':trip});
container.instance().create();
//在此处暂停同步测试,并让任何排队回调执行
等待承诺;
期望(间谍)与(旅行)一起被逮捕;
expect(container.state('trip')).toEqual({});
expect(container.state('errors')).toEqual([]);
});
测试中的trip
对象似乎只有名称
属性。您需要传递一个响应
和响应。状态
属性,因为这是您的承诺解决的问题,并且您正在检查其在逻辑中的值。我已经编辑了代码,但仍然得到相同的错误@t3\uu rryOk您是否也可以编辑您的问题?我已经这样做了,我已经更改了mock api.js文件@t3\uuu rryGreat谢谢,我没有意识到你可以在没有实例的情况下等待