我正在开发一个AngularJS应用程序。我的一项服务真的很麻烦。出于这个原因,我想测试它。要运行这些测试,我想使用一个繁琐的任务。我的想法是,我会从命令行输入grunt test
来运行我的单元测试。我的第一步是创建我的grunt文件,如下所示:
gruntfile.js
'use strict';
module.exports = function (grunt) {
grunt.initConfig({
jasmine: {
mocks: {
src: 'dist/service.min.js',
options: {
specs: 'test/*.js',
vendor: [
'bower_components/angularjs/angular.min.js',
'bower_components/angular-mocks/angular-mocks.js'
]
}
}
}
});
require('load-grunt-tasks')(grunt);
grunt.registerTask('test', ['jasmine:unitTests']);
};
然后,我的测试看起来像这样:
service.spec.js
describe('myModule', function() {
beforeEach(function() {
module('myModule');
});
describe('$myService', function () {
var service = null;
beforeEach(inject(function ($myService) {
service = $myService;
}));
it('should get here', function () {
console.log('first test');
var isSynchronized = service.isSynchronized();
expect(isSynchronized).toBe(true);
});
});
})
此测试旨在测试我的服务:
service.js
'use strict';
angular.module('myModule', [])
.factory('$myService', [function () {
return {
isSynchronized: function () {
return true;
}
};
}]
);
我的问题与测试中的inject
命令有关。该函数调用导致测试中断。如果我删除使用inject的beforeEach块,测试至少会运行。我一直听说Karma。但是,我对此并不了解。
Karma是否需要测试AngularJS服务?我找不到从grunt测试ngularJS服务的示例。我看到的一切都在浏览器中。我试图创建允许我测试AngularJS服务的最低要求的grunt文件。但是,我完全陷入困境。我无法弄清楚为什么注射失败。
有人可以告诉我我错过了什么。或者,或许请指出一个使用grunt来测试AngularJS服务的示例?目前,我在命令行中看到以下内容,因此我知道service.js存在。
Running "uglify:mocks" (uglify) task
Verifying property uglify.mocks exists in config...OK
Files: dist/service.js -> dist/service.min.js
Options: banner="", footer="", compress, mangle={}, beautify=false, report="min", expression=false, preserveComments=false
Minifying with UglifyJS...Reading dist/service.js...OK
OK
Writing dist/service.min.js...OK
File dist/service.min.js created: 293 B → 190 B
Running "jasmine:mocks" (jasmine) task
Verifying property jasmine.mocks exists in config...OK
Files: dist/service.min.js
Options: version="2.0.1", timeout=10000, styles=[], specs="test/*.js", helpers=[], vendor=["bower_components/angularjs/angular.min.js","bower_components/angular
-mocks/angular-mocks.js"], outfile="_SpecRunner.html", host="", template="C:\\source\\myModule\\node_modules\\grunt-contrib-jasmine\\tasks/jasmine
/templates/DefaultRunner.tmpl", templateOptions={}, junit={}, ignoreEmpty=false, display="full", summary=false
Reading C:\source\myModule\node_modules\grunt-contrib-jasmine\tasks\lib/../jasmine/reporters/PhantomReporter.js...OK
Writing .grunt\grunt-contrib-jasmine\reporter.js...OK
Reading C:\source\myModule\node_modules\grunt-contrib-jasmine\tasks\lib/../../vendor/jasmine-2.0.1/jasmine.css...OK
Writing .grunt\grunt-contrib-jasmine\jasmine.css...OK
Reading C:\source\myModule\node_modules\grunt-contrib-jasmine\tasks\lib/../../vendor/jasmine-2.0.1/jasmine.js...OK
Writing .grunt\grunt-contrib-jasmine\jasmine.js...OK
Reading C:\source\myModule\node_modules\grunt-contrib-jasmine\tasks\lib/../../vendor/jasmine-2.0.1/jasmine-html.js...OK
Writing .grunt\grunt-contrib-jasmine\jasmine-html.js...OK
Reading C:\source\myModule\node_modules\grunt-contrib-jasmine\tasks\lib/../../vendor/jasmine-2.0.1/boot.js...OK
Writing .grunt\grunt-contrib-jasmine\boot.js...OK
Reading C:\source\myModule\node_modules\grunt-contrib-jasmine\tasks\lib/../../vendor/jasmine-2.0.1/jasmine_favicon.png...OK
Writing .grunt\grunt-contrib-jasmine\jasmine_favicon.png...OK
Reading C:\source\myModule\node_modules\grunt-contrib-jasmine\tasks\lib/../../node_modules/es5-shim/es5-shim.js...OK
Writing .grunt\grunt-contrib-jasmine\es5-shim.js...OK
Reading C:\source\myModule\node_modules\grunt-contrib-jasmine\tasks/jasmine/templates/DefaultRunner.tmpl...OK
Processing source...OK
Writing _SpecRunner.html...OK
Testing jasmine specs via phantom
谢谢!