最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular Jasmine test not finding modules - Stack Overflow

programmeradmin0浏览0评论

So my test is plaining that it can't find ui.bootstrap

INFO [karma]: Karma v0.10.2 server started at http://localhost:8080/
INFO [launcher]: Starting browser Chrome
WARN [watcher]: Pattern "/home/xenoterracide/lm/frontend/test/mock/**/*.js" does not match any file.
INFO [Chrome 30.0.1599 (Linux)]: Connected on socket 15lSt3HPpk9b-rKPvQzY
Chrome 30.0.1599 (Linux) Controller: Week should attach days of the week to scope FAILED
    Error: No module: ui.bootstrap
        at Error (<anonymous>)
        at /home/xenoterracide/lm/frontend/app/bower_ponents/angular/angular.js:1211:17
        at ensure (/home/xenoterracide/lm/frontend/app/bower_ponents/angular/angular.js:1152:38)
        at module (/home/xenoterracide/lm/frontend/app/bower_ponents/angular/angular.js:1209:14)
        at /home/xenoterracide/lm/frontend/app/bower_ponents/angular/angular.js:2904:24
        at Array.forEach (native)
        at forEach (/home/xenoterracide/lm/frontend/app/bower_ponents/angular/angular.js:130:11)
        at loadModules (/home/xenoterracide/lm/frontend/app/bower_ponents/angular/angular.js:2900:5)
        at /home/xenoterracide/lm/frontend/app/bower_ponents/angular/angular.js:2905:38
        at Array.forEach (native)
    TypeError: Cannot read property 'days_of_the_week' of undefined
        at null.<anonymous> (/home/xenoterracide/lm/frontend/test/spec/controllers/week.js:20:17)
Chrome 30.0.1599 (Linux): Executed 1 of 1 (1 FAILED) ERROR (0.126 secs / 0.017 secs)
Warning: Task "karma:unit" failed. Use --force to continue.

Aborted due to warnings.

Elapsed time
concurrent:test  1s

here's my app.js which is where I load ui.bootstrap.

'use strict';

angular.module('lmApp', [
    'ui.bootstrap',
    'ui.router'
])
.config(['$stateProvider', '$urlRouterProvider',
    function ( $stateProvider, $urlRouterProvider ) {
            $urlRouterProvider.otherwise('/')
            $stateProvider.state('index', {
                    url: "", // root
                    views: {
                            "Nav":        { templateUrl: "views/nav.html" },
                            "Week":       { templateUrl: "views/week.html" },
                    },
            })
    }
])
.factory('now', function () { return new Date })
;

here's my test

'use strict';

describe('Controller: Week', function () {

  // load the controller's module
  beforeEach(module('lmApp'));

  var MainCtrl,
    scope;

  // Initialize the controller and a mock scope
  beforeEach(inject(function ($controller, $rootScope) {
    scope = $rootScope.$new();
    MainCtrl = $controller('Week', {
      $scope: scope
    });
  }));

  it('should attach days of the week to scope', function () {
    expect(scope.days_of_the_week.length).toBe(7);
  });
});

So my test is plaining that it can't find ui.bootstrap

INFO [karma]: Karma v0.10.2 server started at http://localhost:8080/
INFO [launcher]: Starting browser Chrome
WARN [watcher]: Pattern "/home/xenoterracide/lm/frontend/test/mock/**/*.js" does not match any file.
INFO [Chrome 30.0.1599 (Linux)]: Connected on socket 15lSt3HPpk9b-rKPvQzY
Chrome 30.0.1599 (Linux) Controller: Week should attach days of the week to scope FAILED
    Error: No module: ui.bootstrap
        at Error (<anonymous>)
        at /home/xenoterracide/lm/frontend/app/bower_ponents/angular/angular.js:1211:17
        at ensure (/home/xenoterracide/lm/frontend/app/bower_ponents/angular/angular.js:1152:38)
        at module (/home/xenoterracide/lm/frontend/app/bower_ponents/angular/angular.js:1209:14)
        at /home/xenoterracide/lm/frontend/app/bower_ponents/angular/angular.js:2904:24
        at Array.forEach (native)
        at forEach (/home/xenoterracide/lm/frontend/app/bower_ponents/angular/angular.js:130:11)
        at loadModules (/home/xenoterracide/lm/frontend/app/bower_ponents/angular/angular.js:2900:5)
        at /home/xenoterracide/lm/frontend/app/bower_ponents/angular/angular.js:2905:38
        at Array.forEach (native)
    TypeError: Cannot read property 'days_of_the_week' of undefined
        at null.<anonymous> (/home/xenoterracide/lm/frontend/test/spec/controllers/week.js:20:17)
Chrome 30.0.1599 (Linux): Executed 1 of 1 (1 FAILED) ERROR (0.126 secs / 0.017 secs)
Warning: Task "karma:unit" failed. Use --force to continue.

Aborted due to warnings.

Elapsed time
concurrent:test  1s

here's my app.js which is where I load ui.bootstrap.

'use strict';

angular.module('lmApp', [
    'ui.bootstrap',
    'ui.router'
])
.config(['$stateProvider', '$urlRouterProvider',
    function ( $stateProvider, $urlRouterProvider ) {
            $urlRouterProvider.otherwise('/')
            $stateProvider.state('index', {
                    url: "", // root
                    views: {
                            "Nav":        { templateUrl: "views/nav.html" },
                            "Week":       { templateUrl: "views/week.html" },
                    },
            })
    }
])
.factory('now', function () { return new Date })
;

here's my test

'use strict';

describe('Controller: Week', function () {

  // load the controller's module
  beforeEach(module('lmApp'));

  var MainCtrl,
    scope;

  // Initialize the controller and a mock scope
  beforeEach(inject(function ($controller, $rootScope) {
    scope = $rootScope.$new();
    MainCtrl = $controller('Week', {
      $scope: scope
    });
  }));

  it('should attach days of the week to scope', function () {
    expect(scope.days_of_the_week.length).toBe(7);
  });
});
Share Improve this question asked Oct 16, 2013 at 19:30 xenoterracidexenoterracide 16.9k24 gold badges128 silver badges258 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

The problem is stated in the error log when it says:

Error: No module: ui.bootstrap

Based on that, I'm going to guess you're missing a step in your Karma config file (usually called karma.conf.js). You need to supply all of the necessary libraries that your app requires in your config so that Karma knows to load those up in memory before running your tests. When you call beforeEach(module('lmApp'));, Karma tries to create your lmApp module, but can't because one (or more) of its declared dependencies isn't available.

I'm 99% sure that if you just include the source code for ui.bootstrap in the files: {} portion of your karma config, it should start working.

See the karma docs here for more info.

发布评论

评论列表(0)

  1. 暂无评论