0
私はVueコンポーネントmodal
を持っています。これは親によって渡されたonClose
のpropを持っています。 ok
ボタンをクリックすると、onClose
メソッドがトリガされます。コードは以下の通りである:vueコンポーネントの受け渡し方法をテストする方法
Vue.component('modal', {
template: '#modal-template',
props: {
onClose: {
type: Function
}
},
methods: {
ok() {
this.onClose();
}
}
})
// start app
new Vue({
el: '#app',
methods: {
close() {
\t alert('close')
}
}
})
<script src="https://npmcdn.com/[email protected]/dist/vue.min.js"></script>
<script type="x/template" id="modal-template">
<div class="modal-mask">
<button @click='ok()'> OK </button>
</div>
</script>
<div id="app">
<modal :on-close="close" ></modal>
</div>
Iはmodal
コンポーネントのユニットテストはclose
方法にspy
を定義する必要があり、そうテストは以下のようでなければならないと思う:
let vm = new Vue({
template: '<div><modal v-ref:test-component :on-close="close"></modal></div>',
methods: {
close: sinon.spy()
},
components: { ConfirmModal }
}).$mount()
const modal = vm.$refs.testComponent
modal.ok()
expect(vm.close).have.been.called()
エラーでテストに失敗しました:TypeError: [Function] is not a spy or a call to a spy!