【发布时间】:2015-06-29 22:03:45
【问题描述】:
我是 karma-jasmine 的新手,正在尝试开发演示测试用例。我在 'it' 中收到 scope is not defined 错误。我已经阅读了以下具有相同问题的链接,但它对我的测试用例没有帮助。
Error Karma 'undefined' scope variable in beforeEach
TypeError: $scope is undefined in Angular controller unit test with Jasmine
这是我的 karma.conf.js
module.exports = function(config) {
config.set({
exclude)
basePath: '',
frameworks: ['jasmine'],
files: [
'js/angular.js',
'js/angular-mocks.js',
'app.js',
'test/**/*Spec.js'
],
exclude: [
],
preprocessors: {
},
reporters: ['progress'],
port: 9876,
colors: true,
config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Firefox'],
singleRun: false
});
};
这是我编写测试代码的mySpec.js
describe('myApp', function() {
beforeEach(module('myApp'));
describe('HelloWorldController', function() {
var scope,HelloWorldController;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
HelloWorldController = $controller('HelloWorldController', {
$scope: scope
});
}));
it('should assign message to hello world', function () {
expect(scope.greeting).toEqual('Hello World!');
});
});
});
这是我定义控制器的 app.js。
var myApp = angular.module('myApp',[]);
myApp.controller('HelloWorldController', ['$scope', function($scope) {
$scope.greeting = 'Hello World!';
}]);
我得到的错误是,
TypeError: 范围在**中未定义 /home/abc/WebstormProjects/test1/test/basic/mySpec.js(第 17 行)
我不知道我在哪里犯了错误。请指导我解决这个问题。
提前致谢。
【问题讨论】:
-
我不知道实际的问题是什么,但是当我使用 bower 安装角度文件时,它解决了我的问题。因此,如果您没有使用 bower 安装 angular 文件,则它可能无法正常工作。谢谢。
标签: angularjs unit-testing jasmine karma-jasmine