[英]Testing service with angular-mocks/jasmine - TypeError: undefined is not an object
Im trying to test a simple service in my angular app, using jasmine/karma/phantomJS. 我试图在我的角度应用程序中测试一个简单的服务,使用jasmine / karma / phantomJS。
jasmine version: 2.4.1 angular/angular-mocks: 1.5.7 phantomJS: 2.1.1 茉莉花版本:2.4.1角度/角度模拟:1.5.7幻像:2.1.1
QueryParameters.service.tests.js : (QueryParameters.service.js is part of the app.service module, and is actually a factory , not a service) QueryParameters.service.tests.js :( QueryParameters.service.js是app.service模块的一部分,实际上是工厂 ,而不是服务)
describe('myApp.QueryParametersService', function() {
var QueryParametersService;
beforeEach(module('myApp'));
beforeEach(module('app.service'));
beforeEach(inject(function ($injector) {
QueryParametersService = $injector.get('QueryParametersService');
}));
var testObject = {
name : 'Hans',
age : '27'
};
it('Should output correct querystrings', function() {
expect(QueryParametersService.toQueryParams(testObject)).toBe('?name=Hans&age=27');
});
});
QueryParametersService.js: QueryParametersService.js:
(function() {
'use strict';
angular.module('app.service')
.factory('QueryParametersService', QueryParametersService);
QueryParametersService.$inject = [];
function QueryParametersService() {
var service = {
toQueryParams : toQueryParams
};
return service;
function toQueryParams(queryObject) {
<removed code here>
}
}
})();
In Gruntfile.js: 在Gruntfile.js中:
karma: {
unit: {
options: {
frameworks: ['jasmine'],
singleRun: true,
browsers: ['PhantomJS'],
files: [
'<%= yeoman.client %>/bower_components/angular/angular.js',
'<%= yeoman.client %>/bower_components/angular-mocks/angular-mocks.js',
'<%= yeoman.client %>/app/app.js',
'<%= yeoman.client %>/app/filters/filter.module.js',
'<%= yeoman.client %>/app/directives/directive.module.js',
'<%= yeoman.client %>/app/services/service.module.js',
'<%= yeoman.client %>/app/services/tests/*.js'
]
}
}
}
my main module (in app.js) is declared like this: 我的主模块(在app.js中)声明如下:
angular.module('myApp', [
'angular-thumbnails',
'angularUtils.directives.dirPagination',
'app.directive',
'app.filter',
'app.service',
'color.picker',
'ngCookies',
'ngFileUpload',
'ngImgCrop',
'ngMaterial',
'ngMessages',
'ngResource',
'ngSanitize',
'ui.router',
'validation.match',
])
The error im getting when running the tests: 运行测试时得到的错误:
PhantomJS 2.1.1 (Mac OS X 0.0.0) myApp.QueryParametersService Should output correct querystrings FAILED
/<filepath>/client/bower_components/angular/angular.js:4632:53
forEach@/<filepath>/client/bower_components/angular/angular.js:321:24
loadModules@/<filepath>/client/bower_components/angular/angular.js:4592:12
createInjector@/<filepath>/client/bower_components/angular/angular.js:4514:30
workFn@/<filepath>/client/bower_components/angular-mocks/angular-mocks.js:3067:60
TypeError: undefined is not an object (evaluating 'QueryParametersService.toQueryParams') in /<filepath>/client/app/services/tests/query-parameters.service.tests.js (line 65)
/<filepath>/client/app/services/tests/query-parameters.service.tests.js:65:34
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.002 secs / 0.009 secs)
Where am I going wrong here? 我在哪里错了?
As @jchen86 pointed out, QueryParametersService.js wasn't included in the configuration (I thought it would be included when I included the whole service module). 正如@ jchen86指出的那样,QueryParametersService.js没有包含在配置中(我认为当我包含整个服务模块时它会包含在内)。 Solved by changing the gruntfile to include all js files in the service folder:
通过更改gruntfile以包含服务文件夹中的所有js文件来解决:
'<%= yeoman.client %>/app/services/*.js'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.