fetch-mock拦截并模拟数据遇到的问题
mocha测试时用fetch-mock拦截并模拟数据但是报
待测试方法:
export function deletebale(insertData) {
return function (dispatch) {
let url = hostUrl+'/manager/user/add';
fetchPost(url,insertData,function(result){
console.log('Fetch result:',result)
})
}
}
mocha测试代码:
describe('action userTableAction', ()=> {
describe('action init', ()=> {
it('Should be done when fetch action init', (done)=> {
const data = {
"code": 200,
"msg": "ok",
"data": {
"about": "it's init fetch init"
}
};
const acSuccess = {
type: userTableActions.INIT_USER,
userList: data.data
};
const expectedActions = [
acSuccess
];
//拦截 post请求并返回自定义数据
fetchMock.mock(hostUrl+`/manager/user/getlist?query={list(page_size:1000){id,user_name,pass_word,email,account}}`,data);
const store = mockStore({});
store.dispatch(userTableActions.init());
done()
//比较store.getActions()与期望值
expect(store.getActions()).to.deep.equal(expectedActions);
});
});
describe('action insert', ()=> {
it('Should be done when fetch action insert', (done)=> {
const data = {
"code": 200,
"msg": "ok",
"data": {
"about": "it's init fetch insert"
}
};
const acSuccess = {
type: userTableActions.ADD_USER,
userList: data.data
};
const expectedActions = [
acSuccess
];
//拦截 post请求并返回自定义数据
fetchMock.mock(hostUrl+`/manager/user/add`,data,{method:'POST'});
const store = mockStore({});
store.dispatch(userTableActions.insert());
done()
//比较store.getActions()与期望值
expect(store.getActions()).to.deep.equal(expectedActions);
});
});
describe('action deleteTable', ()=> {
it('Should be done when fetch action detory', (done)=> {
const data = {
"code": 200,
"msg": "ok",
"data": {
"about": "it's init fetch deleteTable"
}
};
const acSuccess = {
type: userTableActions.DELETE_USER,
userList: data.data
};
const expectedActions = [
acSuccess
];
//拦截 post请求并返回自定义数据
let url=hostUrl+'/user/destory'
console.log('url',url);
fetchMock.mock(url,data,{method:'POST'});
const store = mockStore({});
store.dispatch(userTableActions.deleteTable(2));
done()
//比较store.getActions()与期望值
expect(store.getActions()).to.deep.equal(expectedActions);
});
});
测试代码中init和insert是可以测试通过的但是deletable就用过不了,报图片上的错误,hostUrl="http:192.168.12.100",如果把deletable中url的hostUrl去掉就不会报错,这是什么问题?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你好 这个东西怎么配置