我不知道如何找到编写此集成测试的方法。 我正在使用 enzyme 来模拟 React 组件,使用 jest 进行测试,使用 nock 来模拟 axios api 调用。
到目前为止,我创建了模拟点击按钮的测试,我想模拟 api 调用。
在互联网上没有太多帮助。
我的测试:
it('Should invoke clear action and clear the group', (done) => {
// GIVEN
const clearButtonComponent = wrapper.find('[id="123"]');
nock('http://localhost:8080')
.intercept('/path/api/brum/123/group', 'DELETE')
.reply(200, {
status: 200,
message: 'cleared',
});
const service = new myService();
// WHEN
clearButtonComponent.first().simulate('click');
const result = Promise.resolve(service.clearGroup(123));
// THEN
expect(result).toEqual({ x: 'x' }); // I know it's not what I expect
wrapper.update();
done();
});
异步 Action 还原:
export const clearGroup = id=> (dispatch, getState) => {
myService.clearGroup(id)
.then(() => {
return dispatch(getGroup(id))
});
};
我的服务中的方法:
clearGroup(id) {
return this._delete(`/${id}/group`);
}
当然路径更复杂,但我的服务扩展了具有此基本 url 的基本服务。
谁能告诉我如何模拟它让代码更进一步?
它仍然提示 id 未定义 - 看起来 nock 没有模拟它。
最佳答案
我会放弃 nock(这些天我尝试只将它用于测试客户端)并开玩笑地模拟 myService
。
我不使用 axios,所以没用过这个,但它可能会起作用.. https://github.com/knee-cola/jest-mock-axios .
否则你可以考虑编写自己的模拟.. https://jestjs.io/docs/en/es6-class-mocks
关于reactjs - 集成测试 - redux/react + nock.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52818771/