Reactjs 测试Redux异步操作创建者不调用.then()
我正在尝试测试Redux操作创建者,但无法获得返回的执行承诺的.then。我使用Moxios模拟来自Axios的HTTP请求。以下是Moxios设置:Reactjs 测试Redux异步操作创建者不调用.then(),reactjs,redux,jestjs,axios,moxios,Reactjs,Redux,Jestjs,Axios,Moxios,我正在尝试测试Redux操作创建者,但无法获得返回的执行承诺的.then。我使用Moxios模拟来自Axios的HTTP请求。以下是Moxios设置: import moxios from "moxios" import backend from "../../utils/backend"; beforeEach(() => { moxios.install(backend); }); afterEach(() => { mo
import moxios from "moxios"
import backend from "../../utils/backend";
beforeEach(() => {
moxios.install(backend); });
afterEach(() => {
moxios.uninstall(backend); });
以下是测试此动作创建者的规范:
it("Creates GET_TRENDING_TOPICS_SUCCESS when fetching trending topics is successful", () => {
moxios.stubRequest("/trending-topics/", {
status: 200,
response: {
data: {
results: []
}
}
});
const store = mockStore(initialState);
store
.dispatch(actions.getTrendingTopics())
.then(() => {
// Nothing in here is executed...
expect(store.getActions())
.toEqual([{
type: "GET_TRENDING_TOPICS_SUCCESS",
payload: []
}]);
});
});
我检查了操作是否返回承诺,并且没有创建错误。有人看到我在这里做错了什么吗?谁负责API调用?如果您使用的是中间件,是否已将其注册到模拟存储区?请尝试使用nock进行测试;)我遇到了类似的问题,通过切换到
async
/wait
而不是then
,绕过了它。谁负责进行API调用?如果您使用的是中间件,是否已将其注册到模拟存储区?请尝试使用nock进行测试;)我遇到了类似的问题,并通过切换到async
/wait
而不是then
绕过了它。