Angular联合测试工厂注射不起作用

时间:2016-02-04 22:19:52

标签: angularjs jasmine karma-jasmine

首先,我的Karma / Jasmine设置正常。 我有其他测试运行,例如一个简单的控制器和一个$ http的控制器。 但我的问题是将以下工厂注入规范。

注意:工厂是这里发布的简化版本,是我必须与之合作的其他人的代码。其他一切都是我的代码

var app = angular.module("myApp", []);
    app.factory('LanguagesService', function(){
        var lng = {},
            _languages = ['en', 'es', 'fr'];

        lng.get = function() {
            return _languages;
        }

        return lng;
    });

我的规格是

describe('Testing Languages Service', function(){
    var LanguagesService;

    beforeEach(function(){
        module('myApp');
        inject(function($injector){
            LanguagesService = $injector.get('LanguagesService');
        });
    });

    it('should return available languages', function() {
        var languages = LanguagesService.get();
        expect(languages).toContain('en');
        expect(languages).toContain('es');
        expect(languages).toContain('fr');
        expect(languages.length).toEqual(3);
    });
});

我的Gulp文件是

var gulp = require('gulp');

gulp.task('singleRun', function (done) {
    startTests(true, done);
});

gulp.task('alwaysRun',  function (done) {
    startTests(false, done);
});

function startTests(singleRun, done) {
    var Server = require('karma').Server;
    var excludeFiles = [];

    new Server({
        configFile: __dirname + '/karma.conf.js',
        singleRun: !!singleRun
    }).start();

    function karmaCompleted(karmaResult) {
        if (karmaResult === 1) {
            done('karma: tests failed with code ' + karmaResult);
        } else {
            done();
        }
    }
};

我的karma.conf文件是

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: [
        'node_modules/angular/angular.min.js',
        'node_modules/angular-mocks/angular-mocks.js',
        'app/\*.js',
        'tests/\*.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
  })
}

当我运行规范测试时,我得到以下异常

Chrome 48.0.2564 (Windows 10 0.0.0) Testing Languages Service should return available languages FAILED
        Error: [$injector:unpr] http://errors.angularjs.org/1.4.9/$injector/unpr?p0=LanguagesServiceProvider%20%3C-%20LanguagesService
            at Error (native)
            at F:/Development/Angular/AngularTest/test1/node_modules/angular/angular.min.js:6:416
            at F:/Development/Angular/AngularTest/test1/node_modules/angular/angular.min.js:41:144
            at Object.d [as get] (F:/Development/Angular/AngularTest/test1/node_modules/angular/angular.min.js:39:129)
            at F:/Development/Angular/AngularTest/test1/node_modules/angular/angular.min.js:41:218
            at Object.d [as get] (F:/Development/Angular/AngularTest/test1/node_modules/angular/angular.min.js:39:129)
            at Object.<anonymous> (F:/Development/Angular/AngularTest/test1/tests/testLanguageFactory.spec.js:35:42)
            at Object.e [as invoke] (F:/Development/Angular/AngularTest/test1/node_modules/angular/angular.min.js:39:431)
            at Object.workFn (F:/Development/Angular/AngularTest/test1/node_modules/angular-mocks/angular-mocks.js:2517:20)
            at window.inject.angular.mock.inject (F:/Development/Angular/AngularTest/test1/node_modules/angular-mocks/angular-mocks.js:2489:37)
        Error: Declaration Location
            at window.inject.angular.mock.inject (F:/Development/Angular/AngularTest/test1/node_modules/angular-mocks/angular-mocks.js:2488:25)
            at Object.<anonymous> (F:/Development/Angular/AngularTest/test1/tests/testLanguageFactory.spec.js:34:9)
        TypeError: Cannot read property 'get' of undefined
            at Object.<anonymous> (F:/Development/Angular/AngularTest/test1/tests/testLanguageFactory.spec.js:40:41)

我在这里做了些蠢事,任何人都可以看到它是什么吗?

0 个答案:

没有答案