使用Jasmine以uibModal和lodash作为依赖项来测试服务

did*_*man 6 javascript jasmine angularjs angular-ui-bootstrap lodash

这是我第一次使用Jasmine,我已经测试了我的第一个工厂没有问题.

但是现在,我想测试这个服务:

angular.module('Questions', [])
.service('QuestionsService', function($uibModal, $log, _) { 
  ... 
}
Run Code Online (Sandbox Code Playgroud)

$ uibModal来自UI Bootstrap(见这里),_是Lodash.

到目前为止我的茉莉花测试是:

describe('Service: QuestionsService', function() {

    var QuestionsService;

    beforeEach(inject(function(_QuestionsService_) {
      QuestionsService = _QuestionsService_;
    }));

    ...
}
Run Code Online (Sandbox Code Playgroud)

当我尝试它(咕噜声测试)时,我收到以下错误:

错误:[$ injector:unpr]未知提供者:$ uibModalProvider < - $ uibModal < - QuestionsService

在某些时候我也有:

错误:[$ injector:unpr]未知提供者:_Provider < - _ < - QuestionsService

如果它可以提供帮助,我的Karma conf是:

module.exports = function(config) {
  'use strict';
  config.set({
    autoWatch: true,
    basePath: '../',

    frameworks: [
      "jasmine"
    ],

    // list of files / patterns to load in the browser
    files: [
      // bower:js
      'bower_components/jquery/dist/jquery.js',
      'bower_components/lodash/lodash.js',
      'bower_components/angular/angular.js',
      'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap.js',
      'bower_components/angular-animate/angular-animate.js',
      'bower_components/angular-cookies/angular-cookies.js',
      'bower_components/angular-resource/angular-resource.js',
      'bower_components/angular-route/angular-route.js',
      'bower_components/angular-sanitize/angular-sanitize.js',
      'bower_components/angular-touch/angular-touch.js',
      'bower_components/angular-bootstrap/ui-bootstrap-tpls.js',
      'bower_components/angular-mocks/angular-mocks.js',
      // endbower
      "app/scripts/**/*.js",
      "test/mock/**/*.js",
      "test/spec/**/*.js",
    ],
    exclude: [
    ],
    port: 8080,
    browsers: [
      "PhantomJS"
    ],
    plugins: [
      "karma-phantomjs-launcher",
      "karma-jasmine"
    ],
    singleRun: false,
    colors: true,
    logLevel: config.LOG_INFO,
  });
};
Run Code Online (Sandbox Code Playgroud)

gne*_*kus 0

该应用程序的模块未包含在测试中。重构的测试将QuestionService是:

describe('Service: QuestionsService', function() {

    var QuestionsService;

    // The module needs to be included in the test.
    beforeEach(module('boardgameApp'));

    beforeEach(inject(function(_QuestionsService_) {
      QuestionsService = _QuestionsService_;
    }));

    ...
}
Run Code Online (Sandbox Code Playgroud)