如何在测试Vuejs组件时以编程方式设置$ route.params

时间:2018-02-14 12:23:13

标签: javascript vuejs2 vue-router

根据$route.params$route.query,我有一个Vue.js组件具有不同的行为。类似的东西:

<template>
  <div class="hello">
    {{query}}
  </div>
</template>

<script>
export default {
  name: 'hello',
  data () {
    return {

      }
    }
  },
  computed: {
    'query': () => {
      return $route.params.query
    }
  }
}
</script>

如何编写单元测试规范,以编程方式设置$route.params$route.query的值?

这样的事情:

import Vue from 'vue'
import Template from '@/components/Template'

describe('Template.vue', () => {
  it('should render according to query', () => {
    const Constructor = Vue.extend(Script)
    const vm = new Constructor()
    vm.$route = { // THIS DOES NOT WORK
      params: {
        id: 1      
      }
    }
    vm.$mount()
    // Test contents
  })
})

失败
setting a property that has only a getter

0 个答案:

没有答案