gpt4 book ai didi

javascript - Vuex:使用 API 调用测试操作

转载 作者:行者123 更新时间:2023-11-29 15:23:19 26 4
gpt4 key购买 nike

我一直在关注these测试我的 vuex 商店的测试指南。但是当我接触到 Action 部分时,我觉得有很多事情是我无法理解的。

第一部分是这样的:

// 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)
}
}
})

我推断这是在action里面mock service。

接下来的部分是:

// 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"

我需要帮助来了解我应该做什么来测试此类操作。

最佳答案

我知道已经有一段时间了,但我遇到了这个问题,因为我遇到了类似的问题。如果您在调用 testAction 之前访问 console.log injectedActions,您会看到 injectedAction 对象实际上如下所示:

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

所以这里的主要解决方案是将 testAction 调用更改为:

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

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

与您的特定错误无关的其他一些问题...您不需要操作 testAction 样板代码。只要您传入正确的参数,它就会按预期工作。另外,一定要将 done 传递给 testAction,否则您的测试将超时。希望这对遇到此问题的其他人有所帮助!

关于javascript - Vuex:使用 API 调用测试操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41532266/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com