简体   繁体   English

如何解决 vuex 和单元测试的问题?

[英]How can I solve problem with vuex and unit-testing?

I am trying to write and run unit-testing first time with mocha and faced some problem.我正在尝试第一次使用 mocha 编写和运行单元测试,但遇到了一些问题。 I have a component Table.vue wrote with vuetify我有一个用Table.vue编写的组件 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>

And I want to run test there.我想在那里进行测试。 I have file table.spec.js inside tests/unit folder我在tests/unit文件夹中有文件table.spec.js

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

When I run command npm run test:unit I have error 1) Table.vue renders h1 tag: TypeError: Cannot read property 'table' of undefined previously it was error with text Cannot read property 'state' of undefined当我运行命令npm run test:unit我有错误1) Table.vue renders h1 tag: TypeError: Cannot read property 'table' of undefined previous it was error with text Cannot read property 'state' of undefined

I tried to include vuex inside tests, but it is my first time with unit-test and looks like I do something wrong我试图在测试中包含 vuex,但这是我第一次使用单元测试,看起来我做错了什么

I need my test to be passed and maybe someone can help me to write new test for some event, for example click button and call action.我需要通过我的测试,也许有人可以帮助我为某些事件编写新测试,例如单击按钮并调用操作。 Thanks in advance提前致谢

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

They have great examples of mocking vuex, which is what you need to do.他们有 mocking vuex 的很好的例子,这是你需要做的。 It looks like this:它看起来像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM