Javascript 业力+角度未定义误差
我刚开始用karma和jasmine进行角度测试。我写了两个基本测试。两次测试中有一次通过了,但另一次失败了。我似乎无法调试它。我已经到处找过了,根据各种教程,它应该可以工作。$scope变量应该可用。我收到以下错误和大量文本,请参见:Javascript 业力+角度未定义误差,javascript,angularjs,unit-testing,karma-runner,karma-jasmine,Javascript,Angularjs,Unit Testing,Karma Runner,Karma Jasmine,我刚开始用karma和jasmine进行角度测试。我写了两个基本测试。两次测试中有一次通过了,但另一次失败了。我似乎无法调试它。我已经到处找过了,根据各种教程,它应该可以工作。$scope变量应该可用。我收到以下错误和大量文本,请参见: at C:/totalflatline-2.0/public/lib/angular/angular.js:4362 at forEach (C:/totalflatline-2.0/public/lib/angula
at C:/totalflatline-2.0/public/lib/angular/angular.js:4362
at forEach (C:/totalflatline-2.0/public/lib/angular/angular.js:336)
at loadModules (C:/totalflatline-2.0/public/lib/angular/angular.js:4364)
at createInjector (C:/totalflatline-2.0/public/lib/angular/angular.js:4248)
at workFn (C:/totalflatline-2.0/public/lib/angular-mocks/angular-mocks.js:2409)
at C:/totalflatline-2.0/public/lib/angular-mocks/angular-mocks.js:2392
at C:/totalflatline-2.0/public/shared/tests/unit/shared.client.controller.unit.tests.js:
5
at C:/totalflatline-2.0/node_modules/karma-jasmine/lib/boot.js:126
at C:/totalflatline-2.0/node_modules/karma-jasmine/lib/adapter.js:171
at http://localhost:9876/karma.js:210
at http://localhost:9876/context.html:83
TypeError: 'undefined' is not an object (evaluating '$scope.test')
at C:/totalflatline-2.0/public/shared/tests/unit/shared.client.controller.unit.tests.js:
9
at C:/totalflatline-2.0/node_modules/karma-jasmine/lib/boot.js:126
at C:/totalflatline-2.0/node_modules/karma-jasmine/lib/adapter.js:171
at http://localhost:9876/karma.js:210
at http://localhost:9876/context.html:83
hantomJS 1.9.8 (Windows 7): Executed 2 of 2 (1 FAILED) (0.402 secs / 0.025 secs)
这两项测试如下。第一个通过,另一个给出上述未定义的错误
通过的那个:
describe('Testing MEAN Main Module', function() {
var mainModule;
beforeEach(function() {
mainModule = angular.module('mean');
});
it('Should be registered', function() {
expect(mainModule).toBeDefined();
console.log('Success!');
});
});
失败的一个:
describe('Testing the Shared Stats controller', function(){
var SharedController,$scope;
beforeEach(function(){
// load the module you're testing.
module('mean');
inject(function($rootScope,$controller){
// create a scope object for us to use.
$scope = $rootScope.$new();
SharedController = $controller('shared.StatsController',{
$scope:$scope
});
});
});
it('Should contain a user object',function(){
// User cannot be undefined
expect($scope.test).toEqual('yoo');
});
});
角度控制器如下所示:
// Create the 'shared' controller
angular.module('shared').controller('shared.StatsController', [ '$scope',
function($scope) {
$scope.test = 'yoo';
}
]);
角度版本为1.4,业力相关性为:
"karma": "~0.12.23",
"karma-jasmine": "~0.2.2",
"karma-phantomjs-launcher": "~0.1.4",
我一整天都在为这事生气。我希望有更多关于测试角度的知识的人能帮助我 您的角度控制器未正确实例化。要“获取”或加载模块,请使用angular.module“moduleName”[],第二个参数是具有依赖项的数组。因此,它应该是:
angular.module('shared', []).controller('StatsController', [ '$scope',
function($scope) {
$scope.test = 'yoo';
}
]);