【发布时间】:2019-11-13 16:21:12
【问题描述】:
我有一个这样的js代码:
const Repository = axios.create( {
baseURL: process.env.VUE_APP_API_HOST
} );
const state = { ... }
const getters = { ... }
const mutations = { ... }
const actions = {
fetch: async ( { commit, getters }, { apiPath, id } ) => {
const response = await Repository.get( apiPath );
commit( 'setArticleList', { response.data, id } );
commit( 'changeApiStatus', { status: 'success', id } );
}
};
export const articleList = {
namespaced: true,
state,
getters,
mutations,
actions
};
我有一个这样的笑话代码:
import { store as store2 } from './store';
describe( 'store article_list test: ', () => {
let store;
beforeEach( () => {
store = new Vuex.Store( store2 );
} );
describe( 'test actions', () => {
test( 'get value correctly', async () => {
const apiPath = 'api/v1/article_list.json';
const id = 1;
await store.dispatch( 'articleList/fetch', { apiPath, id } );
} );
} );
} );
这是商店代码:
store.js
import Vue from 'vue';
import Vuex from 'vuex';
import { articleList } from '@/store/modules/article_list';
Vue.use( Vuex );
export const store = {
modules: {
articleList
}
};
export default new Vuex.Store( store );
await store.dispatch( 'articleList/fetch', { apiPath, id } );
目前此代码有一个Error: Error: connect ECONNREFUSED 127.0.0.1: 8080 错误
我想嘲讽 这部分 const response = await Repository.get( apiPath );
但我不知道如何在 vuex 存储中用 jest 模拟变量。
如果你知道如何模拟这部分,请告诉我。
感谢您阅读我的问题。
【问题讨论】: