我刚刚开始使用Karma
&发现角度单元测试的奥秘,Jasmine
而且我在controllers
测试方面遇到了一些问题。我编写了测试来查看是否定义了变量:expect(scope.someVariable).toBeDefined()
. 一切都按预期工作(每个测试都成功),直到我添加ng-scenario
到karma.conf.js
文件中。
我node
用来运行测试。
问题:在将ng-scenario
业力配置文件添加为框架后,所有用于检查是否定义范围变量的测试都失败了。添加之前ng-scenario
所有的测试都成功了。你知道为什么会这样吗?
业力配置文件:
module.exports = function(config) { config.set({ basePath: '', frameworks: ['ng-scenario','jasmine'], files: [ 'app/plugins/jquery/jquery.js', 'app/plugins/angular/angular.js', 'app/plugins/angular-scenario/angular-scenario.js', 'app/plugins/angular-mocks/angular-mocks.js', 'app/plugins/angular-resource/angular-resource.js', 'app/plugins/angular-cookies/angular-cookies.js', 'app/plugins/angular-sanitize/angular-sanitize.js', 'app/plugins/angular-route/angular-route.js', 'app/plugins/angular-utf8-base64/angular-utf8-base64.js', 'app/scripts/*.js', 'app/scripts/**/*.js', 'test/**/*.js', 'app/views/templates/*.html' ], preprocessors : { 'app/views/templates/*.html': 'html2js' }, exclude: ['app/plugins/angular-scenario/angular-scenario.js'], port: 8080, logLevel: config.LOG_INFO, autoWatch: false, browsers: ['Chrome'], singleRun: false }); };
控制器测试:
'use strict' describe('Controller: MainCtrl', function () { beforeEach(module('qunizGameApp')); var MainCtrl,scope, configService,feedbackService,authService,notify,resourceService; beforeEach(inject(function ($controller, $rootScope,_configService_,_feedbackService_, _authService_,_notify_, _resourceService_) { scope = $rootScope.$new(); MainCtrl = $controller('MainCtrl', { $scope: scope, configService:_configService_, feedbackService:_feedbackService_, authService:_authService_, notify:_notify_, resourceService:_resourceService_ }); configService=_configService_; feedbackService=_feedbackService_; authService=_authService_; notify=_notify_; resourceService=_resourceService_; })); it('should be defined -configService', function () { expect(scope.configService).toBeDefined(); }); it('should be defined - resourceService', function () { expect(scope.resourceService).toBeDefined(); }); it('should be defined - sidebarVisible', function () { expect(scope.sidebarVisible).toBeDefined(); }); });
包括之前的工作测试证明ng-scenario
:
包括以下内容后的故障测试证明ng-scenario
:
奇怪的是,如果我在chrome
控制台中调试测试,就会定义变量:
实习和尝试的事情:
1.我尝试在beforeEach
部分中将范围内的变量定义为模拟数据。
2.我尝试注入所有其他控制器依赖项(使用未定义的名称作为参数)
您可以在下面看到 1 和 2
beforeEach(inject(function ($controller, $rootScope,_configService_,_feedbackService_, _authService_,_notify_, _resourceService_) { scope = $rootScope.$new(); scope.configService=_configService_; scope.authService=_authService_; scope.resourceService=_resourceService_; scope.sidebarVisible={}; MainCtrl = $controller('MainCtrl', { $scope: scope, configService:_configService_, feedbackService:_feedbackService_, authService:_authService_, notify:_notify_, resourceService:_resourceService_ }); configService=_configService_; feedbackService=_feedbackService_; authService=_authService_; notify=_notify_; resourceService=_resourceService_; }));
但是scope.configService
,scope.resourceService
和scope.sidebarVisible
在测试时仍然未定义