节点/ vue.jsunit testing的承诺,如何正确写入?

基本上,我试图testing下面的vex / action:

export default { login (context, payload) { return vueAuthInstance.login(payload.user, payload.requestOptions) .then((response) => { if (JSON.stringify(response.data) !== '{}') { return true } else { return false } }) } } 

我有一个依赖vueAuthInstance我需要存根/模拟…它被调用,它有一个login函数,有两个参数,它返回一个对象(响应)与数据属性

  vueAuthInstance = {login: sinon.stub().withArgs({email: 'email', password: 'password'}, 'options').return({data: 'user_data_with_token'}) } 

我的login函数实际上就像调用vueAuthInstance存根,所以我应该可以写

 actions.login(context, payload).then((response) => { expect(response).to.eql(true) }) 

但我被locking在一个正确的testing

 describe('login', () => { const payload = {user: {email: 'john.doe@domain.com', password: 'john123'}} sinon.stub(vueAuthInstance, 'login').withArgs(payload).returns(true) it('should return successful login', () => { actions.login(context, payload).then((response) => { expect(response).to.eql(true) }) }) }) 

我得到一个错误:

  actions.js login TypeError: Cannot read property 'then' of undefined 

完成上下文,这里是完整的文件…

@ / services / auth.js =======================

 import Vue from 'vue' import Vuex from 'vuex' aimport { VueAuthenticate } from 'vue-authenticate' import axios from 'axios' Vue.use(Vuex) Vue.use(VueAxios, axios) const vueAuthInstance = new VueAuthenticate(axios, { baseUrl: '/' }) a export default vueAuthInstance 

@ / vuex / getters.js =======================

 import _ from 'underscore' export default { isAuthenticated: state => state.isAuthenticated, } 

@ / vuex / mutation_types.js =======================

 export const IS_AUTHENTICATED = 'isAuthenticated' export const CURRENT_USER_ID = 'currentUserId' 

@ / vuex / actions.js =======================

  import { IS_AUTHENTICATED, CURRENT_USER_ID } from './mutation_types' import getters from './getters' import vueAuthInstance from '@/services/auth.js' login (context, payload) { payload = payload || {} return vueAuthInstance.login(payload.user, payload.requestOptions) .then( login: ({ commit }, payload) => { payload = payload || {} return vueAuthInstance.login(payload.user, payload.requestOptions) .then((response) => { // check response user or empty if (JSON.stringify(response.data) !== '{}') { commit(IS_AUTHENTICATED, { isAuthenticated: true }) commit(CURRENT_USER_ID, { currentUserId: response.data.id }) return true } else { commit(IS_AUTHENTICATED, { isAuthenticated: false }) commit(CURRENT_USER_ID, { currentUserId: '' }) return false } }) } 

/test/unit/specs/vuex/actions.js =======================

  describe('login', () => { it('should return successful login', () => { }) }) 1/ I must stub or spy my dependency vueAuthInstance 

正如@Bergi所说,返回的对象应该是一个承诺,所以正确的testing是:

  describe('loginUser', () => { var sandbox, payload, response beforeEach(() => { sandbox = sinon.sandbox.create() }) afterEach(() => { sandbox.restore() }) it('should return successful login', () => { payload = {user: {email: 'john.doe@domain.com', password: 'john123'}, requestOptions: {}} response = {data: {id: 1, name: 'John Doe', email: 'john.doe@domain.com', created_at: '2017-11-02T07:56:36.405Z', token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjMiLCJuYW1lIjoiSm9obiBMb2dpbiBEb2UiLCJhZG1pbiI6dHJ1ZX0.VHK2sCbj5nKJ8oC44UTHuhQHXEdPN8zjKjaFT0OZ-L'}} sandbox.stub(vueAuthInstance, 'login').withArgs(payload.user, payload.requestOptions).returns(Promise.resolve(response)) return actions.login(store, payload) .then((result) => { expect(result).to.eql(true) expect(store.commit).to.have.been.calledWith(types.IS_AUTHENTICATED, { isAuthenticated: true }) expect(store.commit).to.have.been.calledWith(types.CURRENT_USER_ID, { currentUserId: response.data.id }) }) }) it('should return invalid login', () => { payload = {user: {email: 'john.doe@domain.com', password: 'john999'}, requestOptions: {}} response = {data: {}} sandbox.stub(vueAuthInstance, 'login').withArgs(payload.user, payload.requestOptions).returns(Promise.resolve(response)) return actions.login(store, payload) .then((result) => { expect(result).to.eql(false) expect(store.commit).to.have.been.calledWith(types.IS_AUTHENTICATED, { isAuthenticated: false }) expect(store.commit).to.have.been.calledWith(types.CURRENT_USER_ID, { currentUserId: '' }) }) }) })