2

在这个测试中,我只是试图断言该end属性是在被测组件上设置的(DateRangeEditor)。该组件假定this.$refs.datepicker是一个实例HotelDatepicker并调用它的setCheckOutsetCheckIn方法(如果有更好的方法可以做到这一点,请告诉我!)。

该组件在浏览器中正常加载时工作,但我的测试setEndDate失败并出现“TypeError:this.$refs.datepicker.setCheckIn is not a function”。

我是否误解了该stubs选项应该做什么设置?我认为像这样的场景,调用代码只需要调用存根函数而不需要特定的结果,正是存根的用途。我只是用错了吗?

这是我的测试:

import { mount, shallow } from 'vue-test-utils'
import DateRangeEditor from '../../assets/src/components/DateRangeEditor.vue'

describe('DateRangeEditor', () => {

  describe('@checkOutChanged', () => {
    it('sets a new end date', () => {
      const wrapper = shallow(DateRangeEditor, {
        stubs: ['datepicker']
      })

      // simulate user changing the end date
      const date = new Date(2017, 11, 1)
      wrapper.vm.$refs.datepicker.$emit('checkOutChanged', date)

      expect(wrapper.vm.end).to.equal(date)
    })
  })

  describe('@checkInChanged', () => {
    it('sets a new start date', () => {
      const wrapper = shallow(DateRangeEditor, {
        stubs: ['datepicker']
      })

      // simulate user changing the start date
      const date = new Date(2017, 11, 1)
      wrapper.vm.$refs.datepicker.$emit('checkInChanged', date)

      expect(wrapper.vm.start).to.equal(date)
    })
  })
})

这是我的(非常简化的)DateRangeEditor组件:

<template>
  <div class="date-range-editor-container">
    <datepicker
      @checkInChanged="setStartDate"
      @checkOutChanged="setEndDate"
      ref="datepicker"
    ></datepicker>
    <button type="button" @click="save" class="btn save-btn">Save</button>
    <button type="button" @click="cancel" class="btn cancel-btn">Cancel</button>
  </div>
</template>

<script>
import HotelDatePicker from 'vue-hotel-datepicker'

export default {
  methods: {
    setStartDate: function(newDate) {
      if (newDate) {
        // make sure newDate has an hour component
        newDate.setHours((new Date()).getHours())
        this.$refs.datepicker.setCheckIn(newDate)
        this.start = newDate
      }
    },
    setEndDate: function(newDate) {
      if (newDate) {
        // make sure newDate has an hour component
        newDate.setHours((new Date()).getHours())
        this.$refs.datepicker.setCheckOut(newDate)
        this.end = newDate
      }
    },
    save: function() {
      this.$emit('save', this.$data)
    },
    cancel: function() {
      this.$emit('cancel', this.$data)
    },
  },
  data: function() {
    return {
      start: '',
      end: '',
    }
  },
  components: {
    datepicker: HotelDatePicker,
  }
}
</script>

<style>
</style>

最后,这是我的 setup.js 引导文件:

/* globals global */
require('jsdom-global')()
const chai    = require('chai')
const sinon   = require('sinon')

// use Sinon test spies/stubs/mocks in all tests
chai.use(require('chai-sinon'))

// expose libs to all tests
global.chai     = chai
global.sinon    = sinon
global.expect   = chai.expect

版本 n 东西

  • @vue/test-utils ^1.0.0-beta.10
  • Vue ^2.4.4
4

1 回答 1

5

stubs替换子组件方法。这就是您收到错误的原因。

如果要检查是否正在调用某个方法,可以使用这些方法创建一个组件并将其传递到stubs选项中。

const datepicker = {
    setCheckIn: () => {},
    setCheckOutDate: () => {},
    render: () => {}
}

it('sets a new end date', () => {
  const wrapper = shallow(DateRangeEditor, {
    stubs: {
      datepicker
    }
  })

  const date = new Date(2017, 11, 1)
  wrapper.vm.$refs.datepicker.$emit('checkOutChanged', date)
  expect(wrapper.vm.end).to.equal(date)
})
于 2017-12-20T08:09:09.950 回答