1

我正在使用以下茉莉花测试用例

'use strict';

describe('companyService', function() {

    var $httpBackend, companyService;

    beforeEach(angular.mock.module('myApp'));

    beforeEach(angular.mock.inject(function(_$httpBackend_ ,  _companyService_) {
        $httpBackend = _$httpBackend_;
        companyService = _companyService_;
    }));

    it('should return a promise for getCompany function', function() {
       // expect(typeof companyService.getCompany('foobar').then).toBe('function');
    }); 

});

我收到以下错误。正如你在上面看到的。我没有在它里面做任何事情

    minErr/<@C:/Users/userone/Documents/myAppPkg/myApp/WebApiRole/bower_components/angular/angular.js:63:12
       loadModules/<@C:/Users/userone/Documents/myAppPkg/myApp/WebApiRole/bower_components/angular/angular.js:4138:15
       forEach@C:/Users/userone/Documents/myAppPkg/myApp/WebApiRole/bower_components/angular/angular.js:323:11
       loadModules@C:/Users/userone/Documents/myAppPkg/myApp/WebApiRole/bower_components/angular/angular.js:4099:5
       createInjector@C:/Users/userone/Documents/myAppPkg/myApp/WebApiRole/bower_components/angular/angular.js:4025:11
       workFn@C:/Users/userone/Documents/myAppPkg/myApp/WebApiRole/node_modules/angular-mocks/angular-mocks.js:2425:44
irefox 38.0.0 (Windows 8.1): Executed 1 of 1 (1 FAILED) ERROR (0.251 secs / 0.008 secs)

我认为这个问题是因为我一直在使用 angular.mock.inject 方法。因为执行没有进入下面的块

beforeEach(angular.mock.inject(function(_$httpBackend_ , _companyService_) {
        $httpBackend = _$httpBackend_;
        companyService = _companyService_;
    }));

Karma.conf.js 文件

// Karma configuration
// Generated on Thu May 21 2015 16:24:51 

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',
            'node_modules/angular-mocks/angular-mocks.js',
            'app/app.js',
            'app/*.js',
            'app/**/*.js',
            'app/**/**/*.js',
            'app/company/CompanyService.js',
            'test/company/*.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
        hostname: 'localhost',
        port: 44555,


    // 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: ['Firefox'],


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

任何人都可以帮我解决这个问题。?

4

2 回答 2

1

您应该更新 node_module 中的“karma-chrome-launcher”和“karma-firefox-launcher”。这或许可以解决问题。

npm install karma-chrome-launcher

npm install karma-firefox-launcher

如果您认为问题是因为“angular.mock.inject” ,那么可以使用注入功能。

但我认为您的单元测试脚本没有机会加载和执行。所以,这不会导致上述错误。

谢谢。

于 2015-05-26T11:40:29.273 回答
1

我找到了解决这个问题的方法。原来我错过了 karma.conf.js 中的一些依赖项。当您错过依赖项时,$inject 方法不会被执行。我所做的是,从 index.html 复制所有依赖项(从 bower 组件文件夹和 node_modules 文件夹)并将它们放在文件下的 karma.conf.js 中。

例如,我从 index.html 复制了所有文件依赖项并将其放在 files 下

            'bower_components/angular/angular.js',
            'node_modules/angular-mocks/angular-mocks.js',

            'bower_components/jquery/dist/jquery.js',
            'bower_components/pnotify/pnotify.core.js',
            'bower_components/pnotify/pnotify.buttons.js',
            'bower_components/pnotify/pnotify.confirm.js',
            'bower_components/pnotify/pnotify.desktop.js',
            'bower_components/pnotify/pnotify.nonblock.js',
            'bower_components/pnotify/pnotify.callbacks.js',
            'bower_components/pnotify/pnotify.history.js',
            'bower_components/pnotify/pnotify.reference.js',

            'bower_components/jquery-ui/jquery-ui.min.js',
            'bower_components/bootstrap/dist/js/bootstrap.js',

            'js/fullcalendar.min.js',
            'js/daterangepicker.min.js',

            'bower_components/moment/moment.js',
            'bower_components/moment-timezone/builds/moment-timezone-with-data-2010-2020.js',

            'bower_components/angular-i18n/angular-locale_no.js',
            'bower_components/ng-file-upload/angular-file-upload.js',

            'bower_components/angular-ui-router/release/angular-ui-router.js',
            'bower_components/angular-bootstrap/ui-bootstrap-tpls.js',

            'bower_components/angular-translate/angular-translate.js',
            'bower_components/angular-translate-loader-static-files/angular-translate-loader-static-files.js',

            'bower_components/angular-pnotify/src/angular-pnotify.js',

            'bower_components/angular-local-storage/angular-local-storage.js',

            'bower_components/angular-loading-bar/build/loading-bar.min.js',

            'bower_components/angular-moment/angular-moment.js',

            'bower_components/angular-bootstrap-switch/dist/angular-bootstrap-switch.js',
            'bower_components/bootstrap-switch/dist/js/bootstrap-switch.js',

            'app/app.js',
            'app/**/*.js',
            'test/**/*Spec.js'
于 2015-05-27T05:15:31.013 回答