2

Karma&を使用して角度単体テストの謎を発見し始めたばかりで、テストJasmineにいくつかの問題がありcontrollersます。変数が定義されているかどうかを確認するテストを作成しました: expect(scope.someVariable).toBeDefined(). ファイルに追加ng-scenarioするまで、すべてが期待どおりに機能しました(すべてのテストが成功しました) 。karma.conf.js

私はnodeテストを実行するために使用しています。

問題:カルマ構成ファイルをフレームワークとして追加ng-scenarioした後、スコープ変数が定義されているかどうかを確認するすべてのテストが失敗します。ng-scenarioすべてのテストを追加する前に成功しました。なぜこれが起こっているのか分かりますか?

カルマ 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   }); };

コントローラーのテスト:

'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コンソールでテストをデバッグすると、変数が定義されます。

デバッグ テスト

インターンで読んで試したもの:

beforeEach1.セクションのスコープに変数をモックデータとして定義しようとしました。

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.configServicescope.resourceServiceおよびscope.sidebarVisibleは、テストの時点ではまだ定義されていません

4

1 に答える 1

0

Karma で ng-scenario を使用すると、同じ問題が発生しました。それを karma.conf.js ファイルに含めた瞬間、私のテストはどれも合格しませんでした。ng-scenario を使用しないでください。統合テストには分度器を使用してください。https://github.com/angular/protractor

于 2014-12-18T14:47:49.800 に答える