Joe*_*Joe 1 angularjs karma-runner
我的控制器中有这个代码:
angular.module('clientApp')
.controller('UserSettingsAccountCtrl', function ($scope, userFactory) {
$scope.$watch(function() {
return userFactory.getUser();
}, function(value) {
$scope.user = value;
console.log($scope.user) //defined when I add scope.$digest() in my test
}
});
console.log($scope.user) //undefined
});
Run Code Online (Sandbox Code Playgroud)
它是在我的控制器实例化时执行的.但在我的测试中,它不是.我得到用户未定义.
beforeEach(inject(function ($controller, $rootScope, userFactory) {
scope = $rootScope.$new();
var mockUser = {
userId: 1,
name: 'Per',
premium:{
active:false,
freeTrial:{
expired:{
value:false
}
}
}
};
userFactory.setUser(mockUser);
console.log(userFactory.getUser()); //User is returned
UserSettingsAccountCtrl = $controller('UserSettingsAccountCtrl', {
$scope: scope,
});
}));
it('should be ok to start a trial if the trial has not expired and the user is not a premium', function(){
console.log(scope.user) //undefined
...
});
Run Code Online (Sandbox Code Playgroud)
为什么我在现场运行但不在我的测试中执行?
通常,手表是在摘要周期触发的,无论是在初始化时还是由于某些事件.从测试开始,您需要在更改应影响范围或受范围影响的内容后手动运行摘要周期:
$scope.$digest();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2348 次 |
| 最近记录: |