在这个测试中,我只是试图断言在被测组件(gl_Position.z *= gl_Position.w;
)上设置了end
属性。该组件假定DateRangeEditor
是HotelDatepicker
的一个实例,并调用其this.$refs.datepicker
和setCheckOut
方法(如果有更好的方法,请告诉我们!)。< / p>
该组件在浏览器中正常加载时有效,但我对setCheckIn
的测试失败,出现“TypeError:this。$ refs.datepicker.setCheckIn不是函数”。
我是否误解了setEndDate
选项应该做什么设置?我认为这样的场景,调用代码只需调用存根函数而不需要具体结果,正好是存根的用途。我只是错了吗?
这是我的测试:
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
最后,这是我的setup.js引导程序文件:
<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>
答案 0 :(得分:3)
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)
})