简体   繁体   English

angular.mock.inject方法的问题

[英]Issue with angular.mock.inject method

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)

I assume this issue comes because of the angular.mock.inject method i have been using . 我认为出现此问题是因为我一直在使用angular.mock.inject方法。 because the execution does not enter the following block 因为执行未进入以下程序段

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

Karma.conf.js file 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
  });
};

can any one help me out with this . 有人能帮我这个忙吗? ?

You should update the "karma-chrome-launcher" and "karma-firefox-launcher" in node_module. 您应该在node_module中更新“ karma-chrome-launcher”和“ karma-firefox-launcher”。 This may be solve the issue. 这可能是解决问题的方法。

npm install karma-chrome-launcher npm install karma-chrome-launcher

npm install karma-firefox-launcher npm install karma-firefox-launcher

If you think the issue because of "angular.mock.inject " then one can use inject function. 如果您认为问题是由于“ angular.mock.inject”引起的,则可以使用注入功能。

But I think your unit test script is not getting chance for loading and execution.So,this will not reason for above error. 但是我认为您的单元测试脚本没有机会进行加载和执行。因此,这不会导致上述错误。

Thanks. 谢谢。

I found out the solution for this problem. 我找到了解决此问题的方法。 Turns out i have missed some dependencies in the karma.conf.js . 原来我错过了karma.conf.js中的某些依赖项。 When you missed dependencies , the $inject method does not get executed . 错过依赖项时,不会执行$ inject方法。 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复制了所有依赖项(来自bower componants文件夹和node_modules文件夹),并将它们放在files下的karma.conf.js中。

for example , i have copied all the file dependencies from index.html and put it under files 例如,我从index.html复制了所有文件依赖项,并将其放在文件下

            '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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 angular.mock.inject()方法什么时候立即执行? - When does the angular.mock.inject() method execute immediately? Angular Jasmine,angular.mock.inject和inject有什么区别? - Angular Jasmine, What is the difference between angular.mock.inject and inject? 在Karma + Jasmine下使用angular.mock.inject遇到TypeScript AngularJS控制器测试问题 - TypeScript AngularJS controller test issue with angular.mock.inject under Karma + Jasmine angular.mock.module 和 angular.mock.inject 函数在 jasmine 的 beforeAll 中不起作用 - angular.mock.module and angular.mock.inject functions are not working in jasmine's beforeAll 在业力中使用angular.mock.inject给出“ TypeError:无法读取null的属性&#39;$ injector&#39;” - Using angular.mock.inject in karma gives “TypeError: Cannot read property '$injector' of null” 为什么$仅在'angular.mock.module'函数中提供,$ q仅在'angular.mock.inject'函数中可用? - Why is $provide only available in the 'angular.mock.module' function, and $q only available in the 'angular.mock.inject' function? Angular mock无法注入我的模块依赖项 - Angular mock fails to inject my module dependencies 元素上的角度模拟方法 - Angular mock method on element 角注入器可以注入方法吗? - Can the Angular Injector inject a method? 如何在Karma上使用Jasmine将一个模拟依赖项注入到angular指令中 - How do I inject a mock dependency into an angular directive with Jasmine on Karma
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM