我确实阅读了角度官方教程并成功启动了测试。但我试图对一个简单的控制器进行测试(当你点击按钮时出现一个布尔变化),我有以下错误:ReferenceError:找不到变量:$ scope
控制器:
siteLogic = angular.module('siteLogic', []);
siteLogic.controller('siteCtrl', ['$scope',function($scope, initMap) {
$scope.addingSite=false;
$scope.changeAddingSite = function(){
$scope.addingSite= !$scope.addingSite;
};
}]);
测试文件:
describe('$scope initialisation', function() {
beforeEach(module('siteLogic'));
describe('sets the addingSite at the inverse', function() {
var scope, controller;
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
//it's on the next line I have the error indication
controller = $controller('siteCtrl', { $scope: scope });
}));
it('sets true if false', function() {
scope.addingSite = false;
scope.changeAddingSite();
expect(scope.addingSite).toEqual(true);
});
it('sets false if true', function() {
$scope.addingSite = true;
$scope.changeAddingSite();
expect($scope.addingSite).toEqual(false);
});
});
});
karma.config.js
module.exports = function(config) {
config.set({
basePath: '..',
frameworks: ['jasmine'],
files: [
'test/dep/angular-1.3.15/angular.js',
'test/dep/angular-1.3.15/angular-resource.js',
'test/dep/angular-1.3.15/angular-route.js',
'test/dep/angular-1.3.15/angular-mocks.js',
'lib/map/public/angular/*.js',
'test/map/*.js'
],
autoWatch: true,
browsers: ['PhantomJS']
});
};
答案 0 :(得分:2)
在第二组测试更改$scope
到scope
it('sets false if true', function() {
$scope.addingSite = true; // there is no $scope
$scope.changeAddingSite();
expect($scope.addingSite).toEqual(false);
});
var siteLogic = angular.module('siteLogic', []);
siteLogic.controller('siteCtrl', ['$scope',function($scope) {
$scope.addingSite=false;
$scope.changeAddingSite = function(){
$scope.addingSite= !$scope.addingSite;
};
}]);
describe('$scope initialisation', function() {
beforeEach(module('siteLogic'));
describe('sets the addingSite at the inverse', function() {
var scope, controller;
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
//it's on the next line I have the error indication
controller = $controller('siteCtrl', { $scope: scope });
}));
it('sets true if false', function() {
scope.addingSite = false;
scope.changeAddingSite();
expect(scope.addingSite).toEqual(true);
});
it('sets false if true', function() {
scope.addingSite = true;
scope.changeAddingSite();
expect(scope.addingSite).toEqual(false);
});
});
});

<link href="http://safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine.css" rel="stylesheet"/>
<script src="http://safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine-2.0.3-concated.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular-mocks.js"></script>
&#13;