【发布时间】:2018-04-02 04:01:17
【问题描述】:
在对我的组件进行单元测试(使用 TypeScript 和 vue-class-component 编写)时,我使用 Sinon 来存根 API 调用。将存根添加到单元测试后,仍在调用原始方法(不返回存根值)。
it('should set the text to bar', async () => {
const stubbedApiResponse = () => {
return 'bar';
};
sinon.stub(MyComponent.prototype, 'getFoo').callsFake(stubbedApiResponse);
let options = {
template: '<div><my-component></my-component></div>',
components: {'my-component': MyComponent},
store: this.store
};
this.vm = new Vue(options).$mount();
Vue.nextTick(() => {
expect(this.vm.$el.querySelector('.text').textContent).toBe('bar'); // Still equals 'foo'
});
});
我试图存根的方法在组件中的mounted 上调用并设置文本内容。任何帮助将不胜感激,谢谢!
【问题讨论】:
-
你能提供一个最小的 git repo 来调试吗?
标签: typescript vue.js karma-runner sinon sinon-chai