Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在redux中进行单元测试/存根异步调用_Javascript_Unit Testing_Asynchronous_Redux_Contentful - Fatal编程技术网

Javascript 如何在redux中进行单元测试/存根异步调用

Javascript 如何在redux中进行单元测试/存根异步调用,javascript,unit-testing,asynchronous,redux,contentful,Javascript,Unit Testing,Asynchronous,Redux,Contentful,如何正确地对以下redux异步操作进行单元测试 const client=contentful.createClient(clientConfig); 导出常量fetchNavigation=()=>{ 返回调度=>{ 返回client.getEntries({content_type:'navigation'}) 。然后((条目)=>{ console.log('content\u type=navigation'的所有条目) 发送(接收导航(条目)) }) .catch(错误=>{ log

如何正确地对以下redux异步操作进行单元测试

const client=contentful.createClient(clientConfig);
导出常量fetchNavigation=()=>{
返回调度=>{
返回client.getEntries({content_type:'navigation'})
。然后((条目)=>{
console.log('content\u type=navigation'的所有条目)
发送(接收导航(条目))
})
.catch(错误=>{
log(“出了点问题”);
调度(fetchNavigationFailure(错误));
});   
}

}
IMO mock
getEntries
(可能还有
createClient
)似乎是正确的方法: 这取决于如何加载
contentful
sdk。正如我看到的,你正在使用ES模块和Jasmine,对吗

要模拟
getEntries
函数,必须模拟
createClient
,因为无法从测试中访问客户端

我想这可能就是你要找的

我只是写了一个例子

从“contentful”导入contentful;
导出常量fetchNavigation=()=>{
返回(发送)=>{
返回contentful.createClient({accessToken:'fooo',space:'bar'})
.getEntries({content_type:'navigation'})
.然后(()=>{
是的;
})
.catch(error=>console.error('Something出错',error));
};
};我就是这样解决的。
首先,我使用函数initClient()和getClient()将客户机的创建移动到它自己的文件中。该模块称为contentfulClient

然后,我发现可以在sinon中模拟实例化对象的函数:

import*从“/services/contentfulClient”作为contentful导入;
const client=contentful.initClient(clientConfig);
常量导航={
项目:['page1','page2']
};
//返回以导航作为内容的承诺
sinon.stub(客户端,'getEntries')。解析(导航);
//断言
return store.dispatch(actions.fetchNavigation())
.then(()=>{expect(store.getActions()).toEqual(expectedActions)

})
非常感谢您的回答,Stefan。