2017-01-18 4 views
1

Ich bin neu in Karma, also könnte der Fehler sehr einfach sein.Karma läuft nicht Winkeltests wie erwartet

Es ist meine karma.conf.js

Datei
module.exports = function(config) { 
    config.set({ 

    // base path that will be used to resolve all patterns (eg. files, exclude) 
    basePath: '', 


    // frameworks to use 
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 
    frameworks: ['jasmine'], 


    // list of files/patterns to load in the browser 
    files: [ 
     'bower_components/angular/angular.js', 
     'bower_components/angular-mocks/angular-mocks.js', 
     'bower_components/angular-resource/angular-resource.js', 
     'app/index.js', 
     'app/heroDetail.js', 
     'app/*.js', 
     'test/*.js', 
     'app/**/*.js', 
     'test/**/*.js' 
    ], 


    // list of files to exclude 
    exclude: [ 
    ], 


    // preprocess matching files before serving them to the browser 
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 
    preprocessors: { 
    }, 


    // test results reporter to use 
    // possible values: 'dots', 'progress' 
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter 
    reporters: ['progress'], 


    // web server port 
    port: 9876, 


    // enable/disable colors in the output (reporters and logs) 
    colors: true, 


    // level of logging 
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
    logLevel: config.LOG_INFO, 


    // enable/disable watching file and executing tests whenever any file changes 
    autoWatch: true, 


    // start these browsers 
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 
    browsers: ['Chrome'], 


    // Continuous Integration mode 
    // if true, Karma captures browsers, runs the tests and exits 
    singleRun: false, 

    // Concurrency level 
    // how many browser should be started simultaneous 
    concurrency: Infinity 
    }) 
} 

Innen app directory meine js und html Dateien befinden.

enter image description here

Die index.html wie folgt aussieht:

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Example - example-heroComponentSimple-production</title> 

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script> 

    <script src="index.js"></script> 
    <script src="heroDetail.js"></script> 

</head> 
<body ng-app="heroApp"> 
    <!-- components match only elements --> 
<div ng-controller="MainCtrl as ctrl"> 
    <b>Hero</b><br> 
    <hero-detail hero="ctrl.hero"></hero-detail> 
</div> 
</body> 
</html> 

Und index.js wie folgt aussieht:

(function(angular) { 
    'use strict'; 
    angular.module('heroApp', []).controller('MainCtrl', function MainCtrl() { 
     this.hero = { 
      name: 'Miles Bronson' 
     }; 
    }); 
})(window.angular); 

jetzt in meiner test spec Datei Ich habe versucht:

describe('MainController',function(){ 
    var $rootScope, 
     $scope, 
     controller; 
    beforeEach(function(){ 
     module('heroApp'); 

     inject(function($injector){ 
      $rootScope = $injector.get('$rootScope'); 
      $scope = $rootScope.$new(); 
      controller = $injector.get('$controller')('MainCtrl',{$scope: $scope}); 
     }); 
    }); 

    it('should initialize name of the hero',function(){ 
     expect($scope.hero.name).toEqual('Miles Bronson'); 
    }); 

    it('should not pass',function(){ 
     expect($scope.hero.name).toEqual('Milesl Bronsonkk'); 
    }); 
}); 
Jetzt 210

wenn ich karma start karma.conf.js tun, sagt er

$ karma start karma.conf.js 
18 01 2017 19:58:51.928:WARN [karma]: No captured browser, open http://localhost:9876/ 
18 01 2017 19:58:51.943:INFO [karma]: Karma v1.4.0 server started at http://0.0.0.0:9876/ 
18 01 2017 19:58:51.943:INFO [launcher]: Launching browser Chrome with unlimited concurrency 
18 01 2017 19:58:51.953:INFO [launcher]: Starting browser Chrome 
18 01 2017 19:58:54.145:INFO [Chrome 55.0.2883 (Windows 8.1 0.0.0)]: Connected on socket oOKBdNiWr9pVmcqnAAAA with id 12481546 
Chrome 55.0.2883 (Windows 8.1 0.0.0) MainController should initialize name of the hero FAILED 
     TypeError: Cannot read property 'name' of undefined 
      at Object.<anonymous> (test/controllers/main-controller-spec.js:45:27) 
Chrome 55.0.2883 (Windows 8.1 0.0.0) MainController should not pass FAILED 
     TypeError: Cannot read property 'name' of undefined 
      at Object.<anonymous> (test/controllers/main-controller-spec.js:49:27) 
Chrome 55.0.2883 (Windows 8.1 0.0.0): Executed 2 of 2 (2 FAILED) ERROR (0.052 secs/0.038 secs) 

Aber das zweite Recht versagt haben sollte, während die erste bestanden haben sollte? Warum ist dieses unerwartete Verhalten?

Was mache ich falsch?

Der Chrome-Browser nicht auch viel helfen ...

enter image description here

Bitte um Hilfe!

+0

Try console.log ($ scope.hero) - dies sollte ein Objekt ausdrucken, nicht wahr? – rrd

+0

Beide Erwartungen sollten fehlschlagen - Sie vergleichen eine Zeichenkette "$ scope.hero.name" mit "Miles Bronson" und "Milesl Bronsonkk". – alecxe

+0

@alecxe .. ok hab es. Aber selbst nachdem es repariert wurde, heißt es 'Ausgeführt 1 von 1 ERFOLG (0,017 Sekunden/0,002 Sekunden) '. ? – StrugglingCoder

Antwort

1

Ich denke, vielleicht haben Sie eine Verschachtelung Problem und ein zu viele beschreibt haben ...

Sieht aus wie Sie etwas mehr wie diese wollen, wo die Brände before vor jedem Test:

describe('MainController',function(){ 
    var $rootScope, 
     $scope, 
     controller; 
    beforeEach(function(){ 
     module('heroApp'); 

     inject(function($injector){ 
      $rootScope = $injector.get('$rootScope'); 
      $scope = $rootScope.$new(); 
      controller = $injector.get('$controller')('MainCtrl',{$scope: $scope}); 
     }); 
    }); 

    it('should initialize name of the hero',function(){ 
     expect($scope.hero.name).toEqual('Miles Bronson'); 
    }); 

    it('should not pass',function(){ 
     expect($scope.hero.name).toEqual('Milesl Bronsonkk'); 
    }); 
}); 

Darüber hinaus verwenden Sie nicht wirklich den globalen Bereich in Ihrem Controller ... Ich denke, Ihr Test kann viel einfacher in der Einrichtung und Behauptungen sein. Etwas wie:

describe('TestMainController',function(){ 
    beforeEach(module('heroApp')); 

    var $controller; 

    beforeEach(inject(function(_$controller_){ 
     $controller = _$controller_; 
    })); 

    it('should initialize name of the hero',function(){ 
     var controller = $controller('MainController'); 
     expect(controller.hero.name).toEqual('Miles Bronson'); 
    }); 

    it('should not pass',function(){ 
     var controller = $controller('MainController'); 
     expect(controller.hero.name).toEqual('Milesl Bronsonkk'); 
    }); 
}); 

Hier ist ein plunkr: https://plnkr.co/edit/IAs4iYL69E8Nm5uvQDw1?p=preview

+0

Danke für die Antwort. Aber es zeigt immer noch 'Chrome 55.0.2883 (Windows 8.1 0.0.0): Ausführung 1 von 1 SUCCESS (0.013 secs/0.002 secs) '. Es sollte 2 richtig ausführen? – StrugglingCoder

+0

Ja, sollte es 2 von 2 sagen ...vergewissere dich, dass alles richtig verschachtelt ist. –

+0

Danke für die Lösung? Aber jetzt kommt ein anderer Fehler. Könnten Sie bitte die aktualisierte Frage sehen? – StrugglingCoder

Verwandte Themen