vue-resource で API 呼び出しを行う、作成したサービスをテストしようとしています。サービスは呼び出しを行い、コンポーネントを新しいデータで更新する必要がありますが、私のテストでは更新された値を登録しません。vue-cli webpack の例と同じセットアップを使用しており、認証サービスをこのリポジトリに基づいています (残念ながらテストはありません)。
私のサービス:
export default {
login(context, creds){
context.$http.post(LOGIN_URL, creds)
.then((response) => {
//do something else here
}, (response) => {
context.error = response.data.error
}
}
}
私のテスト:
import Vue from 'vue'
import VueResource from 'vue-resource'
Vue.use(VueResource)
import Auth from 'src/auth'
describe('Auth', () => {
it('should throw an error on unsuccessful login', (done) => {
//intercept the api call and force an invalid response
Vue.http.interceptors.unshift((request, next) => {
next(request.respondWidth({error: 'some error'}, {status: 401}));
});
const vm = new Vue({
data: {
error: ''
}
}).$mount()
Auth.login(vm, {email: 'test@test.com', pass: 'test'} )
//this always fails
expect(vm.error).to.equal('some error')
//ive also tried:
vm.$nextTick(() => {
expect(vm.error).to.equal('some error')
//done()
});
//undo our interceptor
Vue.http.interceptors.shift()
}
}
テストを実行すると、「」が「何らかのエラー」に等しいと予想されるため、失敗します。
私の疑いは、vue-resource が promise を使用しているという事実に関連しています。