Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.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
Javascript Can';在注入(?)模拟、jasmine和angular时未发现变量误差_Javascript_Angularjs_Unit Testing_Jasmine - Fatal编程技术网

Javascript Can';在注入(?)模拟、jasmine和angular时未发现变量误差

Javascript Can';在注入(?)模拟、jasmine和angular时未发现变量误差,javascript,angularjs,unit-testing,jasmine,Javascript,Angularjs,Unit Testing,Jasmine,我是Jasmine的Angular单元测试新手,我已经为这段代码奋斗了几个小时,我在这里浏览了很多文章和答案,但是,我找不到解决方案 正如您所看到的,这些服务是模拟的。问题是,下面的代码抛出错误,因为它找不到authenticationService变量。然而,根据我今天收集的信息,它应该被注入到“it”方法中 如果我重写了代码,并且在“it”方法中注入了必要的东西,那么它就会起作用。但是,这是丑陋的,不是应该遵循的方式,因为它会导致样板代码 我做错了什么 我尝试将注入(function($ro

我是Jasmine的Angular单元测试新手,我已经为这段代码奋斗了几个小时,我在这里浏览了很多文章和答案,但是,我找不到解决方案

正如您所看到的,这些服务是模拟的。问题是,下面的代码抛出错误,因为它找不到authenticationService变量。然而,根据我今天收集的信息,它应该被注入到“it”方法中

如果我重写了代码,并且在“it”方法中注入了必要的东西,那么它就会起作用。但是,这是丑陋的,不是应该遵循的方式,因为它会导致样板代码

我做错了什么

我尝试将注入(function($rootScope,$controller)块移动到过度创建mock,但没有帮助,后来我了解到注入中应该记住一个正确的顺序。因此我将它们放回嵌套的descripe块中

测试的目的是检查在触发事件:auth loginRequired时是否调用了authenticationService.authenticate()。我不确定代码的断言部分是否正确。我将在修复上述问题后处理它

description('dilibShell模块规范',函数()
{
var$location、$rootScope、scope、AuthenticationService、AuthService、Common、ShellController;
在每个(模块(‘dilibShell’)之前;
beforeach(函数()
{
AuthenticationService={
身份验证:函数(用户)
{
返回“用户”;
}
}
授权服务={
登录确认:函数(){}
}
普通={
activateController:function(){},
记录器:{
getLogFn:函数(){}
}
}
模块(功能($提供)
{
$provide.value('authenticationService',authenticationService);
});
模块(功能($提供)
{
$provide.value('authService',authService);
});
模块(功能($提供)
{
$PROFECT.value(‘普通’、普通);
});
});
描述('外壳控制器规格',功能()
{beforeach(函数()
{
注入(函数($rootScope,$controller)
{
rootScope=$rootScope;
scope=$rootScope.$new();
ShellController=$controller('ShellController'{
$scope:scope
});
});
});
它('捕获事件:auth loginRequired时,必须调用authenticationService.Authentication。',
(功能()
{
expect(authenticationService.authenticate()).toBe('user');
//安排
//间谍(authenticationService,'authenticate');
范围。$digest();
//表演
rootScope.$broadcast('event:auth loginRequired');
//断言
期望(authenticationService.authenticate).toHaveBeenCalledWith();
}));
});
});
更新

根据答案,我修改了代码,it块如下所示,它工作正常,不计算代码中的另一个错误,但该错误超出了此范围

因此,我的问题是,在注入服务mock的情况下,我必须在特定的it块中调用inject,这是不可避免的

it('捕获事件:auth loginRequired时,必须调用authenticationService.authenticate',
(注入(功能)(authenticationService)
{
expect(authenticationService.authenticate()).toBe('user');
//安排
间谍(authenticationService,'authenticate');
//范围。$digest();
//表演
rootScope.$broadcast('event:auth loginRequired');
//断言
期望(authenticationService.authenticate).toHaveBeenCalledWith();
})));