2016-08-01 26 views
1

Ich bin neu zu testen, haben Angular Erfahrung. Ich entwickelte eine Angular-Anwendung und möchte Tests hinzufügen, um Jasmine und Karma in den Griff zu bekommen. Also habe ich Karma eingerichtet, eine einfache "getGreeting" Funktion zu einem meiner Dienste in meiner Angular App hinzugefügt und eine Testdatei (/test/UtilsService.spec.js) mit dem Jasmine Test hinzugefügt. Es schlägt fehl, weil der Dienst nicht definiert ist (angle-mocks.js wird hinzugefügt). Dies ist mein Code:Winkelprüfung Jasmine schlägt fehl

karma.conf.js:

// 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: [ 
    'node_modules/**/*.js', 
    'app/**/*.js', 
    'test/UtilsService.spec.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 

Testdatei in /test/UtilsService.spec.js:

describe('getGreeting',function(){ 
    var UtilsService; 
    beforeEach(angular.mock.module('app.ontdekJouwTalent')); 
    beforeEach(inject(
     function(_UtilsService_) { 
      UtilsService = _UtilsService_; 
     } 
    )); 
    it('should test a dummy function', 
     function(){ 
      expect(1+1).toEqual(2); 
      expect(UtilsService.getGreeting("Marc")).toEqual("Hello Marc"); 
     } 
    ) 
}); 

Beachten Sie, dass ich Laden auf Kommentar/Prüfung der Service aber laden Sie nur die App: app.ontdekJouwTalent. Die UtilsService in /app/shared/services/UtilsService.js

angular.module('app.ontdekJouwTalent'). 
service('UtilsService',['AppConfig',function(AppConfig){ 
    this.debug = function(data){ 
     if(AppConfig.APPCONSTANTS_ISLOCAL){ 
      return data; 
     } 
    } 

    this.getGreeting = function(name){ 
     return "Hello " + name; 
    } 
}]) 

Die Winkel app anderswo definiert ist - in /app/app.js wie folgt aus:

angular.module('app.ontdekJouwTalent', [ 
    'angular-storage', 
    'ui.bootstrap', 
    'ui.router', 
    'ui.router.modal', 
    'xeditable', 
    'angular-confirm', 
    'ui.select', 
    'ngSanitize', 
    'angular-growl', 
    'ngAnimate' 
]) 

Wenn diese von der Lauf webroot-Verzeichnis (wwwroot) in einem cmd-Fenster mit „Karma beginnen“ ich bekomme

I:\www\ontdekJouwTalent\wwwroot>karma start 
04 08 2016 19:23:32.633:WARN [karma]: No captured browser, open http://localhost:9876/ 
04 08 2016 19:23:32.756:INFO [karma]: Karma v1.1.2 server started at http://localhost:9876/ 
04 08 2016 19:23:32.757:INFO [launcher]: Launching browser Chrome with unlimited concurrency 
04 08 2016 19:23:32.769:INFO [launcher]: Starting browser Chrome 
04 08 2016 19:23:38.634:INFO [Chrome 51.0.2704 (Windows 10 0.0.0)]: Connected on socket /#CXn5vEn8tQBLJ23oAAAA with id 50497607 
Chrome 51.0.2704 (Windows 10 0.0.0) ERROR 
    Uncaught ReferenceError: module is not defined 
    at node_modules/abbrev/abbrev.js:2 

So ‚Modul‘ ist nicht definiert ... ich habe keine Ahnung, wie diese zu bewältigen. Was ist hier falsch?

+0

Lassen Sie uns gemeinsam debuggen. Bitte, setzen Sie 'console.log (UtilsService)' direkt nach einer Zeile 'UtilService = _UtilsService_;' und zeigen Sie uns, was Sie sehen –

+0

Es ist eine Weile her, seit ich in eckigen getestet, aber sicherlich müssen Sie etwas aus dem Service zurückgeben? – Pureferret

+0

Ihr Zusatz unterscheidet sich von dem, was Sie zuvor geschrieben haben. Nimmt 'inject' den sofortigen Funktionsaufruf? Es sollte nicht so sein. –

Antwort

0

Ok, stellt sich heraus, ich musste eine Los weitere Dateien im Abschnitt karma.conf.js Dateien hinzufügen! speziell:

// list of files/patterns to load in the browser 
files: [ 
    'node_modules/angular/angular.js', 
    'node_modules/angular-ui-bootstrap/dist/ui-bootstrap-tpls.js', 
    'node_modules/ui-select/dist/select.js', 
    'node_modules/angular-mocks/angular-mocks.js', 
    'node_modules/angular-growl-v2/build/angular-growl.min.js', 
    'node_modules/angular-ui-router/release/angular-ui-router.min.js', 
    'node_modules/angular-storage/dist/angular-storage.min.js', 
    'node_modules/angular-animate/angular-animate.min.js', 
    'node_modules/angular-sanitize/angular-sanitize.min.js', 
    'node_modules/angular-xeditable/dist/js/xeditable.min.js', 
    'node_modules/angular-ui-router/release/angular-ui-router.min.js', 
    'node_modules/angular-ui-router-uib-modal/angular-ui-router-uib-modal.js', 
    'node_modules/angular-confirm/angular-confirm.js', 
    'node_modules/angular-aria/angular-aria.min.js', 
    'node_modules/angular-messages/angular-messages.min.js', 
    'app/app.js', 

    'app/shared/services/APIService.js', 
    'app/shared/services/SessionService.js', 
    'app/login/AuthService.js', 
    'app/translation/TranslationService.js', 
    'app/shared/services/UtilsService.js', 
    'test/UtilsService.spec.js' 
], 

nicht ganz klar, warum Ich muss diese Dienste umfassen ... mein UtilsService sicher ist sie nicht benötigen. Meine App enthält 7 Dienste, nicht 5. Warum müssen auch keine Controller hinzugefügt werden (App verwendet +9)?