Vuex:使用API​​调用测试操作

时间:2017-01-08 11:38:09

标签: javascript unit-testing vuejs2 vuex

我一直在遵循these测试指南来测试我的vuex商店。 但当我谈到行动部分时,我觉得有很多事情发生,我无法理解。

第一部分如下:

// actions.js
import shop from '../api/shop'

export const getAllProducts = ({ commit }) => {
  commit('REQUEST_PRODUCTS')
  shop.getProducts(products => {
    commit('RECEIVE_PRODUCTS', products)
  })
}
// actions.spec.js

// use require syntax for inline loaders.
// with inject-loader, this returns a module factory
// that allows us to inject mocked dependencies.
import { expect } from 'chai'
const actionsInjector = require('inject!./actions')

// create the module with our mocks
const actions = actionsInjector({
  '../api/shop': {
    getProducts (cb) {
      setTimeout(() => {
        cb([ /* mocked response */ ])
      }, 100)
    }
  }
})

我推断这是模拟操作中的服务。

以下部分是:

// helper for testing action with expected mutations
const testAction = (action, payload, state, expectedMutations, done) => {
  let count = 0

  // mock commit
  const commit = (type, payload) => {
    const mutation = expectedMutations[count]
    expect(mutation.type).to.equal(type)
    if (payload) {
      expect(mutation.payload).to.deep.equal(payload)
    }
    count++
    if (count >= expectedMutations.length) {
      done()
    }
  }

  // call the action with mocked store and arguments
  action({ commit, state }, payload)

  // check if no mutations should have been dispatched
  if (expectedMutations.length === 0) {
    expect(count).to.equal(0)
    done()
  }
}

describe('actions', () => {
  it('getAllProducts', done => {
    testAction(actions.getAllProducts, null, {}, [
      { type: 'REQUEST_PRODUCTS' },
      { type: 'RECEIVE_PRODUCTS', payload: { /* mocked response */ } }
    ], done)
  })
})

这是我发现很难遵循的地方。

我的商店看起来像:

import * as NameSpace from '../NameSpace'
import { ParseService } from '../../Services/parse'

const state = {
  [NameSpace.AUTH_STATE]: {
    auth: {},
    error: null
  }
}

const getters = {
  [NameSpace.AUTH_GETTER]: state => {
    return state[NameSpace.AUTH_STATE]
  }
}

const mutations = {
  [NameSpace.AUTH_MUTATION]: (state, payload) => {
    state[NameSpace.AUTH_STATE] = payload
  }
}

const actions = {
  [NameSpace.ASYNC_AUTH_ACTION]: ({ commit }, payload) => {
    ParseService.login(payload.username, payload.password)
      .then((user) => {
        commit(NameSpace.AUTH_MUTATION, {auth: user, error: null})
      })
      .catch((error) => {
        commit(NameSpace.AUTH_MUTATION, {auth: [], error: error})
      })
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}

这就是我试图测试的方式:

import * as NameSpace from 'src/store/NameSpace'
import AuthStore from 'src/store/modules/authorization'
const actionsInjector = require('inject!../../../../../src/store/modules/authorization')
// This file is present at: test/unit/specs/store/modules/authorization.spec.js 
// src and test are siblings


describe('AuthStore Actions', () => {
  const injectedAction = actionsInjector({
    '../../Services/parse': {
      login (username, password) {
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            if (Math.random() > 0.5) {
              resolve({})
            } else {
              reject({})
            }
          }, 300)
        })
      }
    }
  })

  it('Gets the user profile if the username and password matches', () => {
    const testAction = (action, payload, state, mutations, done) => {
      const commit = (payload) => {
        if (payload) {
          expect(mutations.payload).to.deep.equal(payload)
        }
      }
      action({ commit, state }, payload)
        .then(result => {
          expect(state).to.deep.equal({auth: result, error: null})
        })
        .catch(error => {
          expect(state).to.deep.equal({auth: [], error: error})
        })
    }
    testAction(injectedAction.login, null, {}, [])
  })
})

如果我尝试这样做,我会得到:

"Gets the user profile if the username and password matches"
undefined is not a constructor (evaluating 'action({ commit: commit, state: state }, payload)')
"testAction@webpack:///test/unit/specs/store/modules/authorization.spec.js:96:13 <- index.js:26198:14
webpack:///test/unit/specs/store/modules/authorization.spec.js:104:15 <- index.js:26204:16"

我需要帮助了解我应该做些什么来测试这些行为。

1 个答案:

答案 0 :(得分:1)

我知道已经过了一段时间,但我遇到了这个问题,因为我遇到了类似的问题。如果您在进行testAction调用之前使用console.log injectActions,您会看到injectAction对象实际上看起来像:

Object{default: Object{FUNC_NAME: function FUNC_NAME(_ref) { ... }}}

因此,这里的主要解决方案是将testAction调用更改为:

testAction(injectedAction.default.login, null, {}, [], done)

因为您要将操作导出为商店中的默认值。

与您的特定错误无关的一些其他问题......您无需操纵testAction样板代码。只要您传入适当的参数,它就会按预期工作。另外,请务必将test传递给testAction,否则测试将会超时。希望这可以帮助遇到此事的其他人!