【发布时间】:2015-05-31 16:55:19
【问题描述】:
我刚刚开始使用 karma 和 jasmine 进行角度测试。我写了两个基本测试。两项测试中的一项通过,但另一项未通过。我似乎无法调试它。我一直在寻找,它应该根据各种教程工作。 $scope 变量应该可用。我收到以下错误(还有很多文字,请参阅):
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';
}
]);
Angular 版本是 1.4,业力依赖是版本:
"karma": "~0.12.23",
"karma-jasmine": "~0.2.2",
"karma-phantomjs-launcher": "~0.1.4",
我整天都在为此伤透了脑筋。我希望对测试角度有更多了解的人可以帮助我。
【问题讨论】:
标签: javascript angularjs unit-testing karma-runner karma-jasmine