angular.mock.inject方法的问题 [英] Issue with angular.mock.inject method

查看:129
本文介绍了angular.mock.inject方法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using folloing jasmine test case

'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');
    }); 

});

我收到以下错误。正如你在上面看到的那样。我在阻止内部没有做任何事情。

i am getting the following error . as you can see above . i am not doing anything inside it block .

    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方法。因为执行没有输入以下块

I assume this issue comes because of the angular.mock.inject method i have been using . because the execution does not enter the following block

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

Karma.conf.js文件

Karma.conf.js file

// 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
  });
};

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

can any one help me out with this . ?

推荐答案

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

I found out the solution for this problem. Turns out i have missed some dependencies in the karma.conf.js . When you missed dependencies , the $inject method does not get executed . What i did was , from the index.html , copied all the dependencies (from bower componants folder and node_modules folder) and put them in the karma.conf.js under files .

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

for example , i have copied all the file dependencies from index.html and put it under 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'

这篇关于angular.mock.inject方法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆