【发布时间】:2018-10-15 14:15:06
【问题描述】:
我不知道如何找到编写此集成测试的方法。 我使用酶来模拟反应组件,玩笑来测试和 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();
});
异步操作还原:
export const clearGroup = id=> (dispatch, getState) => {
myService.clearGroup(id)
.then(() => {
return dispatch(getGroup(id))
});
};
myService 中的方法:
clearGroup(id) {
return this._delete(`/${id}/group`);
}
当然路径更复杂,但我的服务扩展了具有此基本 url 的基本服务。
谁能告诉我如何模拟它以让代码更进一步?
它仍然抱怨 id 未定义 - 看起来 nock 没有模拟它。
【问题讨论】:
标签: reactjs testing redux nock