Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

javascript - How to inject controller in Karma test?

I want to run sample test Angular code using Karma.

I try to modify above code to make sure, that controller will be injected to test. Unfortunetly, inject statement doesn't execute. I have Angular and Angular-mocks in version 1.0.8 and Node in version 8.16.2. I can't modify these versions, because my all project may to stop working.

Additionaly, I get error message TypeError: undefined is not an object (evaluating 'spec.$injector') in src/ProjectBundle/Resources/public/assets/vendor/bower_components/angular-mocks/angular-mocks.js (line 1715) workFn@src/ProjectBundle/Resources/public/assets/vendor/bower_components/angular-mocks/angular-mocks.js:1715:15.

In Karma's configuration I have paths to Angular, Angular-mock, JavaScript code and test code. If I try to add path direct to test file (with word require), then test doesn't work.

Below is fragment of example file after modification.

beforeEach(inject(function(_$controller_, _$rootScope_){
        console.info('test');

        $controller = _$controller_;
        $rootScope = _$rootScope_;
    }));

Below is fragment of angular-mock.js file.

window.module = angular.mock.module = function() {
    var moduleFns = Array.prototype.slice.call(arguments, 0);
    return isSpecRunning() ? workFn() : workFn;
    /////////////////////
    function workFn() {
      var spec = getCurrentSpec();
      if (spec.$injector) {
        throw Error('Injector already created, can not register a module!');
      } else {
        var modules = spec.$modules || (spec.$modules = []);
        angular.forEach(moduleFns, function(module) {
          modules.push(module);
        });
      }
    }
  };

Below is fragment of karma-ci.conf.js file.

module.exports = function(config) {
  config.set({
    frameworks: ['jasmine'],
    plugins : ['karma-jasmine', 'karma-phantomjs-launcher'],
    files: [
          'src/ProjectBundle/Resources/public/assets/vendor/bower_components/angular/angular.js'
        , 'src/ProjectBundle/Resources/public/assets/vendor/bower_components/angular-mocks/angular-mocks.js'
        , 'src/ProjectBundle/Resources/public/assets/javascripts/cm/**/*.js' // contains above example file
        , 'src/ProjectBundle/Tests/Karma/Unit/Controllers/AngularCtrlSpec.js' // contains above example test
    ],
    browsers: ['PhantomJS']
  });
};

Below is fragment of package.json file.

"devDependencies": {
    "jasmine": "^3.6.3",
    "jasmine-core": "^3.6.0",
    "karma": "^5.2.3",
    "karma-jasmine": "^4.0.1",
    "karma-phantomjs-launcher": "^1.0.4"
  }

Do you know any solution of my problem?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...