【发布时间】:2018-03-23 15:17:51
【问题描述】:
我正在为我的 vue 应用程序编写单元测试,但由于在 beforeCreate 方法中调用 session.exists 函数而出现问题。那么我如何模拟生命周期函数或 vue.$session.exists。我尝试使用 sinon 来模拟它,但我没有成功?
代码:
beforeCreate: function() {
if (!this.$session.exists()) {
this.$router.push('/')
}
}
错误信息是:
TypeError: "cannot read property 'exists' of undefined"
【问题讨论】:
-
好吧,欢迎向我们展示您尝试了什么。这样,人们就可以更轻松、更快捷地找到出路。
-
你不希望它调用
vue.$session.exists是吗? -
beforeCreate: function () { if (!this.$session.exists()) { this.$router.push('/') } } 错误信息是:TypeError: "cannot read property '存在'未定义"