这是我第一次使用Jasmine,我测试了我的第一个工厂没有问题。
但是现在,我想测试这项服务:
angular.module('Questions', [])
.service('QuestionsService', function($uibModal, $log, _) {
...
}
$ uibModal来自UI Bootstrap(参见here),_是Lodash。
到目前为止我的茉莉花测试是:
describe('Service: QuestionsService', function() {
var QuestionsService;
beforeEach(inject(function(_QuestionsService_) {
QuestionsService = _QuestionsService_;
}));
...
}
当我尝试它(咕噜声测试)时,我收到以下错误:
错误:[$ 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,
});
};
答案 0 :(得分:1)
以防其他人发现这一点。为了在测试指令的控制器时解决错误,我模拟了$ uibModal服务,概念上是这样的:
describe('Service: QuestionsService', function() {
var controller;
beforeEach(inject(function($controller) {
controller = $controller('controllerName', {
$uibModal : {}
});
}));
...
}
如果您正在针对与之交互的控制器函数编写测试,那么$ uibModal可能需要不仅仅是一个空对象。
答案 1 :(得分: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_;
}));
...
}