【发布时间】:2017-08-12 13:42:43
【问题描述】:
在 VueJS 2.4 中,我们可以通过 this.$root 从组件访问根数据,就像在这个 JSFiddle 中一样:
https://jsfiddle.net/bjg2yL1u/1/
如果你点击一个按钮,你可以在控制台看到'橙色',这是一个根数据,不属于触发它的todo-item。
现在我在 Jasmine 测试中。此测试正常工作/运行/呈绿色。
但是 todo-item 组件中的 console.log 输出 'undefined'。
如何在测试中向 this.$root 实例注入数据?
describe("TodoItem", function() {
var sut;
var message = {text:'word'}
beforeEach(function() {
var Constructor = Vue.extend(TodoItem);
sut = new Constructor({
propsData: {
todo: message,
}
}).$mount();
});
it("Should be able to reverse the given word", function() {
// Given
expect(sut.todo.text).toEqual('word');
expect($(sut.$el).find('li').text()).toEqual('word');
//When
sut.reverseMessage();
// Bang !! problem here. 'undefined' is printed, because there is nothing attached to this.$root when inside a test
// Then
expect(sut.todo.text).toEqual('drow');
});
});
【问题讨论】:
标签: javascript testing jasmine vue.js vuejs2