小编Mik*_*ven的帖子

Vue 单元测试失败,因为组件方法调用 this.$route.query - TypeError:无法读取未定义的属性“查询”

长期使用 StackOverflow 的智慧在工作和工作之外,但我第一次发布问题。多么具有里程碑意义!

我正在为大型 Vue 应用程序编写单元测试,我的一个组件使用一种引用 $route 的方法来确定是否正在传入查询参数/如果正在使用该参数,则传入该参数。调用 this.$route.query.article_id 的方法效果很好,但是现在我正在测试中,测试无法识别 this.$route.query

我在使用shallowMount 挂载我的localVue 时尝试模拟$route 对象,如文档中所述,但它不起作用,并且我继续遇到相同的错误。

这是我的组件:

<template>
  <b-container fluid>
    <div class="content-page-header"></div>
    <b-row>
      <b-col cols="3" class="outer-columns text-center" style="color:grey">
        <font-awesome-icon
          :icon="['fas', 'newspaper']"
          class="fa-9x content-page-photo mb-3 circle-icon"
        />
        <br />
        <br />Get practical tips and helpful
        <br />advice in clear articles written
        <br />by our staff's experts.
      </b-col>
      <b-col cols="6" v-if="articlesExist">
        <h1 class="header-text">
          <b>Articles</b>
        </h1>
        <div v-if="!selectedArticle">
          <div v-for="article in articles">
            <article-card :article="article" @clicked="onClickRead" />
            <br />
          </div>
        </div>
        <div v-else>
          <router-link to="articles" …
Run Code Online (Sandbox Code Playgroud)

testing mocha.js chai vue.js vue-router

3
推荐指数
1
解决办法
3814
查看次数

标签 统计

chai ×1

mocha.js ×1

testing ×1

vue-router ×1

vue.js ×1