我该如何解决Vuex和单元测试的问题?

时间:2019-10-20 16:37:40

标签: unit-testing vue.js mocha vuex

我正尝试用Mocha编写和运行单元测试,但遇到了一些问题。 我有一个用vuetify编写的组件Table.vue

    <v-container>
        <h1>Table</h1>
        <v-layout
                text-center
                wrap
        >
            <v-simple-table v-if="table.length">
                <template v-slot:default>
                    <thead>
                    <tr>
                        <th class="text-left">Id</th>
                        <th class="text-left">Name</th>
                        <th class="text-left">Description</th>
                        <th class="text-left">Action</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr v-for="(item, index) in table" :key="index">
                        <td>{{ item.id }}</td>
                        <td>{{ item.name }}</td>
                        <td>{{ item.description }}</td>
                        <td>
                            <v-btn
                                    color="blue"
                                    :to="{name: 'create', params: { item: item, id: item.id }}"
                            >
                                Update
                            </v-btn>
                            <v-btn
                                    color="red"
                                    @click="show(item)"
                            >
                                Delete
                            </v-btn>
                        </td>
                    </tr>
                    </tbody>
                </template>
            </v-simple-table>
        </v-layout>
        <v-btn
                :to="{name: 'create'}"
                color="primary"
        >
            <span class="mr-2">Create</span>
        </v-btn>
        <modal name="hello-world">
            <v-container>
                <v-layout
                        text-center
                        wrap
                >
                    Are you sure you eant to remove this item?
                </v-layout>
                <v-layout
                        text-center
                        wrap
                >
                    <v-btn
                            color="red"
                            @click="deleteItem"
                    >
                        Yes
                    </v-btn>
                    <v-btn
                            color="primary"
                            @click="hide"
                    >
                        No
                    </v-btn>
                </v-layout>
            </v-container>
        </modal>
    </v-container>
</template>

<script>
    import { mapState, mapActions } from 'vuex'
    export default {
        name: "Table",
        data() {
            return {
                itemToRemove: null
            }
        },
        methods: {
            ...mapActions([
                'removeItem'
            ]),
            show (item) {
                this.$modal.show('hello-world');
                this.itemToRemove = item
            },
            hide () {
                this.$modal.hide('hello-world');
                this.itemToRemove = null
            },
            deleteItem () {
                this.removeItem(this.itemToRemove)
                this.hide()
            }
        },
        computed:
            mapState({
            table: state => state.table.table
        })
    }
</script>

<style scoped>

</style>

我想在那里进行测试。我在table.spec.js文件夹中有文件tests/unit

import { expect } from 'chai'
import { shallowMount, createLocalVue } from '@vue/test-utils'
import Vuex from 'vuex'
import Table from '@/components/Table.vue'
const localVue = createLocalVue()
localVue.use(Vuex)
const store = new Vuex.Store({
    state: {}
})

describe('Table.vue', () => {
    it('renders h1 tag', () => {
    const wrapper = shallowMount(Table, {localVue, store})
    expect(wrapper.find('h1').text().equal('Table'))
})
})

运行命令npm run test:unit时出现错误1) Table.vue renders h1 tag: TypeError: Cannot read property 'table' of undefined 以前是文本Cannot read property 'state' of undefined

错误

我试图将vuex包含在测试中,但这是我第一次进行单元测试,看起来我做错了事

我需要通过测试,也许有人可以帮助我为某些事件(例如单击按钮和调用操作)编写新的测试。预先感谢

1 个答案:

答案 0 :(得分:2)

您必须通读https://vue-test-utils.vuejs.org/guides/using-with-vuex.html

他们有模拟vuex的出色示例,这是您需要做的。看起来像这样:

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

const localVue = createLocalVue()

localVue.use(Vuex)

describe('Actions.vue', () => {
  let actions
  let store

  beforeEach(() => {
    actions = {
      actionClick: jest.fn(),
      actionInput: jest.fn()
    }
    store = new Vuex.Store({
      actions
    })
  })

  it('dispatches "actionInput" when input event value is "input"', () => {
    const wrapper = shallowMount(Actions, { store, localVue })
    const input = wrapper.find('input')
    input.element.value = 'input'
    input.trigger('input')
    expect(actions.actionInput).toHaveBeenCalled()
  })