无法将服务注入Angular单元测试

编程入门 行业动态 更新时间:2024-10-23 15:21:14
本文介绍了无法将服务注入Angular单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用以下方式对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单元测试

本文发布于:2023-11-08 14:10:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569591.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   Angular

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!