2

我刚刚开始使用Karma&发现角度单元测试的奥秘,Jasmine而且我在controllers测试方面遇到了一些问题。我编写了测试来查看是否定义了变量:expect(scope.someVariable).toBeDefined(). 一切都按预期工作(每个测试都成功),直到我添加ng-scenariokarma.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 框架的情况下工作

包括以下内容后的故障测试证明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.resourceServicescope.sidebarVisible在测试时仍然未定义

4

1 回答 1

0

将 ng-scenario 与 Karma 一起使用时,我遇到了同样的问题。我将它包含在 karma.conf.js 文件中的那一刻,我的测试都没有通过。只是不要使用 ng-scenario - 使用 Protractor 进行集成测试。https://github.com/angular/protractor

于 2014-12-18T14:47:49.800 回答