이의 내가 아주 기본적인 VUE 수준의 구성 요소 있다고 가정 해 봅시다 :비웃음 뷰 클래스 구성 요소 메소드
<template>
<div>Nothing of interest here</div>
</template>
<script>
import Vue from 'vue';
import Component from 'vue-class-component';
import http from './../modules/http';
@Component
export default class Example extends Vue {
user = null;
errors = null;
/**
* @returns {string}
*/
getUserId() {
return this.$route.params.userId;
}
fetchUser() {
this.user = null;
this.errors = null;
http.get('/v1/auth/user/' + this.getUserId())
.then(response => this.user = response.data)
.catch(e => this.errors = e);
}
}
</script>
을 나는 그래서 난 그냥 './../modules/http'
의존성을 조롱 fetchUser()
방법을 테스트 할 http.get
을 Promise
으로 보내주십시오. 문제는 제 주장에서 URL이 제대로 빌드되고 있는지 확인하고 테스트를 위해 하드 코드 된 변수에서 사용자 ID를 가져와야합니다.
나는 이런 식으로 뭔가를 시도했지만 작동하지 않습니다 불행히도 작동하지 않습니다
import ExampleInjector from '!!vue-loader?inject!./../../../src/components/Example.vue';
const mockedComponent = ExampleInjector({
'./../modules/http': {
get:() => new Promise(/* some logic */)
},
methods: getUserId:() => 'my_mocked_user_id'
});
및 질문은 그래서는 뷰의 문서에서이 특정 아무것도 찾을 수 없습니다, 어떻게 나는 외부 의존성과 클래스 컴포넌트 메소드를 모의하기로되어 있나?
참고 : 나는 userId
로 this.$route.params.userId
잠재적으로뿐만 아니라 다른 곳에서 올 수 (플러스 이것은 단지 예입니다) 조롱하지 않으. 그냥 getUserId
메소드를 조롱하고 싶습니다.
'injection-loader'로 제 질문에서했던 것처럼 외부 모듈을 조롱하여 예제를 완성 할 수 있습니까? –
@FrancescoCasula 단일 파일 구성 요소를 가져올 때를 의미합니까? –
네, 내 질문에 코드의 두 번째 스 니펫처럼'const mockedComponent = ExampleInjector ({...''Vue.component 'extends'에'mockedComponent'를 그냥 전달하면 작동한다고 생각합니다. '호출. 내 경우에는'const component = Vue.component (/*...*/)'와'const vm = new Vue (component);를해야만했다. 그렇지 않으면 작동하지 않을 것이다. 내 시험에는 아무런 문제가 없다. –