Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js 模拟Vue生命周期方法_Vue.js_Sinon - Fatal编程技术网

Vue.js 模拟Vue生命周期方法

Vue.js 模拟Vue生命周期方法,vue.js,sinon,Vue.js,Sinon,我正在为我的vue应用程序编写单元测试,由于调用beforeCreate方法中的session.exists函数,我遇到了问题。那么,如何模拟生命周期函数或vue。$session.exists。我试着用sinon来模仿它,但我没能做到 代码: beforeCreate:function(){ 如果(!this.$session.exists()){ 这是。$router.push(“/”) } } 错误消息是: TypeError:“无法读取未定义的属性“exists” 当mount()in

我正在为我的vue应用程序编写单元测试,由于调用beforeCreate方法中的session.exists函数,我遇到了问题。那么,如何模拟生命周期函数或vue。$session.exists。我试着用sinon来模仿它,但我没能做到

代码:

beforeCreate:function(){
如果(!this.$session.exists()){
这是。$router.push(“/”)
}
}
错误消息是:

TypeError:“无法读取未定义的属性“exists”


mount()
ing时,您可以模拟
$session

const $session = { exists() { return true; } };
const component = mount(MyComponent, {
  mocks: {
    $session
  }
});
下面的演示。

var MyComponent=Vue.component('MyComponent'{
模板:“你好”,
beforeCreate(){console.log('this.$session.exists()-->',this.$session.exists());},
});
//规格代码///////////////////////////////////////////////////////////
var mount=vueTestUtils.mount;
描述('MyComponent',()=>{
它(“成功模拟$session”,()=>{
const$session={exists(){return true;}}
const parent=mount(MyComponent{
模拟:{
$session
}
});
expect(parent.vm.session).not.toBe(未定义);
});
});
//加载jasmine htmlReporter
(功能(){
var env=jasmine.getEnv()
env.addReporter(新的jasmine.HtmlReporter())
env.execute()
}())


好的,欢迎您,请告诉我们您尝试了什么。这样,有人可以更容易、更快地找到解决方法。您不希望它调用
vue.$session.exists
是这样吗?beforeCreate:function(){if(!This.$session.exists()){This.$router.push('/')}}错误消息是:TypeError:“无法读取未定义的'exists'属性”嘿,您尝试过吗?