我正在尝试使用以下方式对Angular服务进行单元测试:
I'm trying to unit test an Angular service using:
- angular 1.3.8
- angular-mocks 1.3.8
- 业力0.13.19
- jasmine 2.4.1
- 节点0.10.33
- 操作系统:Windows 7
- 浏览器:PhantomJS 2.1.3
- angular 1.3.8
- angular-mocks 1.3.8
- karma 0.13.19
- jasmine 2.4.1
- node 0.10.33
- OS: Windows 7
- Browser: PhantomJS 2.1.3
问题是,我想测试的服务( MyService )没有被angular-mocks lib注入测试文件中(即'inject'方法什么都不做)。我的代码如下所示:
The problem is, the service I wish to test (MyService) is not injected in the test file by the angular-mocks lib (i.e. the 'inject' method does nothing). My code looks as follows:
(function() { 'use strict'; angular.module('module', [ 'ngCookies', 'ngSanitize' ]); })();service.js
service.js
(function () { 'use strict'; angular.module('module') .factory('MyService', MyService); MyService.$inject = ['Dependency']; function MyService(Dependency) { return { method: method }; function method() { // do something } } })();service.spec.js
service.spec.js
(function () { 'use strict'; describe('MyService', function () { var deferred; var MyService; var DependencyMock = {}; beforeEach(module('module')); beforeEach(module(function ($provide) { $provide.value('Dependency', DependencyMock); })); beforeEach(inject(function (_MyService_, $q) { MyService = _MyService_; // nothing is injected here deferred = $q.defer(); // nothing is injected here })); it('should be injected', function () { console.log(deferred); // logs 'undefined' expect(MyService).toBeDefined(); // fails }); describe('method', function () { it('should have this method', function () { expect(MyService.method).toBeDefined(); // fails as MyService is undefined expect(typeof MyService.method).toBe('function'); }); }); }); })();karma.conf.js
karma.conf.js
// list of files / patterns to load in the browser files: [ 'libs/angular/angular.js', 'libs/angular-mocks/angular-mocks.js', 'src/js/main.js', 'src/js/services/service.js', 'src/js/tests/unit/service.spec.js' ]package.json
package.json
"devDependencies": { "bower": "1.7.7", "grunt": "0.4.5", "grunt-contrib-clean": "0.7.0", "grunt-contrib-concat": "0.5.1", "grunt-contrib-connect": "0.11.2", "grunt-contrib-copy": "0.8.2", "grunt-contrib-cssmin": "0.14.0", "grunt-contrib-jshint": "0.12.0", "grunt-contrib-less": "1.1.0", "grunt-contrib-uglify": "0.11.0", "grunt-karma": "0.12.1", "grunt-lesshint": "1.1.1", "grunt-ngdocs": "0.2.9", "grunt-processhtml": "0.3.9", "jasmine-core": "2.4.1", "karma": "0.13.19", "karma-cli": "0.1.2", "karma-coverage": "0.5.3", "karma-jasmine": "0.3.6", "karma-phantomjs-launcher": "1.0.0", "karma-spec-reporter": "0.0.24", "phantomjs-prebuilt": "2.1.3" }bower.json
bower.json
"devDependencies": { "angular": "1.3.8", "angular-animate": "1.3.8", "angular-cookies": "1.3.8", "angular-mocks": "1.3.8", "angular-soap": "2.1.1", "bootstrap": "3.3.6", "font-awesome": "4.5.0", "angular-translate": "2.9.0", "angular-sanitize": "1.3.8" }我的猜测是,这与 angular-mocks 。
推荐答案我已经找到了问题所在。 Karma似乎没有包含其中一个Angular模块依赖项,这导致注入不起作用。
I've figured out the problem. It seems that one of the Angular module dependencies was not included by Karma, which caused the 'inject' to not work.
更多推荐
无法将服务注入Angular单元测试
发布评论