【问题标题】:Vue unit testing - textContent doesn't contain expected propsDataVue 单元测试 - textContent 不包含预期的 propsData
【发布时间】:2017-08-03 13:59:57
【问题描述】:

这是我的组件:

<template>
  <div>
    {{serviceConfig.name}}
  </div>
</template>

<script>
export default {
  props: ['serviceConfig']
}
</script>

在我的测试中,我这样设置 serviceConfig:

    it(`should render serviceConfig`, () => {
    const Constructor = Vue.extend(TestMe2);
    const comp = new Constructor({
        propsData: {
            serviceConfig: '{ "name": "Apple" }'
        }
    }).$mount();

    expect(comp.serviceConfig).to.equal('{ "name": "Apple" }'); // successfull
    expect(comp.$el.textContent).to.contain('Apple'); // unsuccessfull

});

但是,如果我更改模板以呈现 {{serviceConfig}} 而不是 serviceConfig.name,它可以工作,但不是我想要的(因为我只想要名称,即“Apple”)。

【问题讨论】:

    标签: javascript vue.js vuejs2 karma-mocha


    【解决方案1】:

    您传递的是字符串,而不是对象,如 serviceConfig

    【讨论】:

      猜你喜欢
      • 2020-08-02
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 2019-10-17
      • 2020-08-04
      相关资源
      最近更新 更多