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
:
そして奇妙なことに、chrome
コンソールでテストをデバッグすると、変数が定義されます。
インターンで読んで試したもの:
beforeEach
1.セクションのスコープに変数をモックデータとして定義しようとしました。
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
は、テストの時点ではまだ定義されていません