5

我一直在尝试对 Vue 组件进行单元测试,但我似乎无法完全弄清楚如何模拟/存根存储调用 API 异步的对象和方法。

这是我们拥有的 Vue 组件的示例:

import { mapState, mapGetters } from 'vuex'
import router from 'components/admin/router'

export default {
name: 'Users',
computed: {
    ...mapState('admin', [
        'users',
    ]),
    ...mapGetters({
        requestInProgress: 'requestInProgress',
    }),
},
data: function() {
    return {
        filterTerm: '',
        usersLoaded: false,
    }
},
methods: {        
    getUsers(filter) {
            this.$store.dispatch('admin/getUserList', filter)
                .then(res => {
                    this.usersLoaded = true
                })
                .catch(e => {
                    this.$toast.error({
                        title: 'Failed to retrieve data',
                        message: this.$options.filters.normaliseError(e),
                    })
                })            
    },
},
mounted() {
    this.getUsers('*')
},

}

这就是我要写的测试。如果不实际尝试断言任何内容,我什至无法让测试干净地运行

import Vue from 'vue'
import { shallowMount } from '@vue/test-utils'
import Users from 'components/admin/pages/user/users.vue'

describe('Users Vue', () => {
    it('Page Should Load', () => {
     const mockResponse = {
          data: [{
            "id": "1",
            "emailAddress": "beakersoft@gmail.com",
            "firstName": "Luke",
            "lastName": "Niland",
            "staffNumber": "12345",
            "phoneNumber": "07707 999999",
            "active": true
        }
    ]};

    let actions
    let store

    beforeEach(() => {
        actions = {
            'admin/getUserList': sinon.stub()                      
                  .returns(Promise.resolve(mockResponse))
        }
        store = new Vuex.Store({
            state: {},
            actions
        })
    })                   

    const wrapper = shallowMount(Users, { store })

    const h5 = wrapper.find('h5')
    expect(h5.text()).toBe('User Administration')  
  })
 })

我倾向于返回的错误是关于未定义的项目,通常在这种情况下$store.dispatchundefined. 我觉得我在某处的嘲笑中遗漏了一些东西,或者getUsers()被召唤到坐骑上的事实正在绊倒它。

4

2 回答 2

2

为了像您在示例中那样测试模拟 Vuex 的 Vue 组件,可以在您正在执行组件store时将模拟传递给 Vue shallowMount,因此:

shallowMount(Users, { store })

但是这个模拟store也需要安装到基本的 Vue 构造函数中。为此,您必须将其传递给 - localVue。AlocalVue是一个作用域的 Vue 构造函数,您可以在测试范围内对其进行更改,而不会影响应用程序中实际使用的全局 Vue 构造函数。

此外,在您的具体情况下,您既没有导入也没有安装 Vuex。

然后,要正确配置您的测试,您需要:

  1. localVue通过调用 Vue Test Utils 实用程序函数创建一个实例createLocalVue并在其上安装 Vuex:
    import { shallowMount, createLocalVue } from '@vue/test-utils'
    import Vuex from 'vuex'

    //creating the local Vue instance for testing
    const localVue = createLocalVue()

    //mounting Vuex to it
    localVue.use(Vuex)
  1. 更改您的shallowMount函数,还将localVue实例添加到有效负载:
 const wrapper = shallowMount(Users, { store, localVue })

有关官方文档参考,请参见此处

关于 Vue 测试的另一个有用资源是这本书(对于您的具体案例,请参阅第 7 章)及其 GitHub 存储库

于 2019-01-11T09:36:21.450 回答
1

您必须为您的测试创建一个本地 Vue 并安装 Vuex 插件:

import { shallowMount, createLocalVue } from '@vue/test-utils'
import Vuex from 'vuex'

const localVue = createLocalVue()

localVue.use(Vuex)

const wrapper = shallowMount(...)

于 2019-01-10T20:36:06.190 回答