使用ng-scenario作为框架以Karma和Jasminetesting控制器时,范围variables未定义
我刚刚开始使用Karma
& Jasmine
发现angularunit testing的奥秘,并且我在controllers
testing中遇到了一些问题。 我已经写了testing,看看是否定义variables: expect(scope.someVariable).toBeDefined()
。 一切按预期工作(每个testing都成功了),直到我在karma.conf.js
文件中添加了ng-scenario
。
我正在使用node
来运行testing。
问题:在karmaconfiguration文件中添加ng-scenario
作为框架之后,所有用于检查范围variables是否定义的testing都将失败。 在添加ng-scenario
之前,所有的testing都成功了。 你有什么想法为什么会发生这种情况?
Karma Conf文件:
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 }); };
控制器testing:
'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
之前的工作testingcertificate:
包含ng-scenario
后的失败testingcertificate:
而一个奇怪的是,如果我在我的chrome
控制台debuggingtesting,variables被定义:
事实上阅读和尝试:
1.我曾试图在作为模拟数据之前的范围内定义variables。
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
在testing的时候还是未定义的
在和Karma一起使用时,我遇到了与ng-scenario相同的问题。 当我把它包含在karma.conf.js文件中的时候,我的testing都没有通过。 只是不要使用ng-scenario – 使用Protractor进行集成testing。 https://github.com/angular/protractor