Angularjs $scope.$on不在Jasmine SpecRunner中工作
我刚刚开始使用Jasmine测试AngularJS应用程序。 我相信我正在SpecRunner上执行所有导入操作,但我得到了一个错误:Angularjs $scope.$on不在Jasmine SpecRunner中工作,angularjs,jasmine,Angularjs,Jasmine,我刚刚开始使用Jasmine测试AngularJS应用程序。 我相信我正在SpecRunner上执行所有导入操作,但我得到了一个错误: TypeError: $scope.$on is not a function 我在我的控制器中使用它,如下所示: app.controller('asstCtrl', function($scope, $uibModal, $interval, $document) { $scope.$on('$includeContentLoaded', func
TypeError: $scope.$on is not a function
我在我的控制器中使用它,如下所示:
app.controller('asstCtrl', function($scope, $uibModal, $interval, $document) {
$scope.$on('$includeContentLoaded', function() {
$scope.doStuff();
});
});
...
我的SpecRunner测试如下所示:
describe('calculator', function(){
beforeEach(module('asst'));
it('basic', inject(function($controller) {
var scope = {},
ctrl = $controller('asstCtrl', {$scope:scope});
expect(scope.x).toBe(1);
}));
});
describe('calculator', function(){
beforeEach(module('asst'));
var rootScope;
beforeEach(inject(function($rootScope) {
rootScope = $rootScope;
}));
it('basic', inject(function($controller) {
var scope = rootScope.$new(),
ctrl = $controller('asstCtrl', {$scope:scope});
expect(scope.x).toBe(1);
}));
});
以下是我的作品:
- public/js/jasmine-2.4.0/jasmine.js
- public/js/jasmine-2.4.0/jasmine-html.js
- public/js/jasmine-2.4.0/boot.js
- public/js/jquery.min.js
- public/js/angular.js public/js/angular-animate.min.js
- public/js/angular-mocks.js
- public/js/ui-bootstrap-tpls-0.14.2.min.js
describe('calculator', function(){
beforeEach(module('asst'));
it('basic', inject(function($controller) {
var scope = {},
ctrl = $controller('asstCtrl', {$scope:scope});
expect(scope.x).toBe(1);
}));
});
describe('calculator', function(){
beforeEach(module('asst'));
var rootScope;
beforeEach(inject(function($rootScope) {
rootScope = $rootScope;
}));
it('basic', inject(function($controller) {
var scope = rootScope.$new(),
ctrl = $controller('asstCtrl', {$scope:scope});
expect(scope.x).toBe(1);
}));
});
您正在创建一个对象并将其作为控制器的作用域注入。在该对象上不会有
$on
方法
而是注入$rootScope
并使用$new
方法创建范围:
var scope = $rootScope.$new(),
ctrl = $controller('asstCtrl', { $scope: scope });