离子茉莉花测试控制器和范围未定义阻止

时间:2017-05-20 16:03:52

标签: javascript angularjs angular ionic-framework jasmine

我正在尝试使用karma-jasmine为现有的Ionic 1应用程序创建此单元测试;每个都是设置和运行,但是当我尝试下面的第一个测试时,var范围和控制器都是未定义的;我已经查看了几个教程和博客,但一切都像我在这里做的那样。

status.tests.js

describe('statusController', function(){
    var $controller, scope;    
    beforeEach(module('clerk'));

    beforeEach(inject(function($rootScope, _$controller_, $stateParams, $localStorage, $ionicPopup, 
            $cordovaGoogleAnalytics, $cordovaNetwork, ItemsService, OrdenService, ClientesService){

        scope = $rootScope.$new();

        // The injector unwraps the underscores (_) from around the parameter names when matching
        $controller = _$controller_('StatusCtrl', { $scope: scope });
        spyOn(scope, 'printReporte');
    }));

    afterEach(function() {
        scope.$destroy();
    });    

    describe('cierre', function(){
        it('cierre ejecutado', function() {
            console.log('scope: ' + JSON.stringify(scope));
            console.log('controller: ' + JSON.stringify($controller));

            expect($controller).toBeDefined();
            expect(scope).toBeDefined();
            expect(scope.printReporte).toHaveBeenCalledTimes(2);
        });    
    });
});

status.js

 angular.module('clerk.controllers')

 .controller('StatusCtrl', function ($scope, $rootScope, $stateParams, $localStorage, $ionicPopup, 
    $cordovaGoogleAnalytics, $cordovaNetwork, ItemsService, OrdenService, ClientesService) {

    $cordovaGoogleAnalytics.trackView('status');

    $scope.printReporte = function(){      
        OrdenService.printReporte($scope.dataStorage.ordenes, $scope.productosVendidos, $scope.totalClientes, $scope.total);
    }
..........
})

控制台日志

[vns@localhost clerk]$ gulp test
(node:30301) fs: re-evaluating native module sources is not supported. If you are using the graceful-fs module, please update it to a more recent version.
[11:59:03] Using gulpfile ~/workspace/clerk/gulpfile.js
[11:59:03] Starting 'test'...
WARN `start` method is deprecated since 0.13. It will be removed in 0.14. Please use 
  server = new Server(config, [done])
  server.start()
instead.
20 05 2017 11:59:04.166:INFO [karma]: Karma v1.7.0 server started at http://0.0.0.0:9876/
20 05 2017 11:59:04.167:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
20 05 2017 11:59:04.174:INFO [launcher]: Starting browser PhantomJS
20 05 2017 11:59:04.432:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket cndxz1n_qRnaz85aAAAA with id 60468258
LOG: 'scope: undefined'
LOG: 'controller: undefined'
PhantomJS 2.1.1 (Linux 0.0.0) statusController cierre cierre ejecutado FAILED
    www/lib/ionic/js/ionic.bundle.js:17918:53
    forEach@www/lib/ionic/js/ionic.bundle.js:13691:24
    loadModules@www/lib/ionic/js/ionic.bundle.js:17878:12
    createInjector@www/lib/ionic/js/ionic.bundle.js:17800:30
    workFn@www/lib/angular-mocks/angular-mocks.js:2922:60
    loaded@http://localhost:9876/context.js:162:17
    Expected undefined to be defined.
    tests/controllers/status.tests.js:26:44
    loaded@http://localhost:9876/context.js:162:17
    Expected undefined to be defined.
    tests/controllers/status.tests.js:27:38
    loaded@http://localhost:9876/context.js:162:17
    TypeError: undefined is not an object (evaluating 'scope.printReporte') in tests/controllers/status.tests.js (line 28)
    tests/controllers/status.tests.js:28:25
    loaded@http://localhost:9876/context.js:162:17
    TypeError: undefined is not an object (evaluating 'scope.$destroy') in tests/controllers/status.tests.js (line 18)
    tests/controllers/status.tests.js:18:14
    loaded@http://localhost:9876/context.js:162:17
PhantomJS 2.1.1 (Linux 0.0.0): Executed 1 of 2 (1 FAILED) (skipped 1) ERROR (0.048 secs / 0.016 secs)
[11:59:04] Finished 'test' after 911 ms

jasmine files

files: [
  'www/lib/ionic/js/ionic.bundle.js',
  'www/lib/angular-mocks/angular-mocks.js',
  'www/lib/ngCordova/dist/ng-cordova.js', 
  'www/lib/ionic/js/angular/angular-resource.js',           
  'www/js/app.js',
  'www/js/controllers.js',
  'www/js/orden.js',
  'www/js/status.js',
  'www/js/cliente.js',
  'www/js/services/clientes.js',
  'www/js/services/items.js',
  'www/js/services/login.js',
  'www/js/services/orden.js',
  //'www/js/**/*.js',
  'tests/**/*tests.js'
]

使用: 离子1.3.1 茉莉花2.6.0 节点版本:6.10.2

更新: karma config

// list of files / patterns to load in the browser
files: [      
  "www/lib/ionic/js/ionic.bundle.js",
  "www/lib/angular/angular.js",
  "www/lib/angular-mocks/angular-mocks.js",      
  "www/lib/ngCordova/dist/ng-cordova.js", 
  'www/lib/ionic/js/angular/angular-resource.js',           
  'www/js/app.js',
  'www/js/services/clientes.js',
  'www/js/services/items.js',
  'www/js/services/login.js',
  'www/js/services/orden.js',            
  'www/js/controllers.js',
  'www/js/orden.js',
  'www/js/status.js',
  'www/js/cliente.js',
  //'www/js/**/*.js',
  'tests/**/*tests.js'
]

每次进样时,还将$ rootScope更改为 $ rootScope 下划线。

app.js

angular.module('clerk', ['ionic', 'ionic.cloud','clerk.controllers', 'clerk.services', 'ngSanitize', 'ngCordova']) ....

0 个答案:

没有答案
相关问题