首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单元测试中的角1.x服务注入

单元测试中的角1.x服务注入
EN

Stack Overflow用户
提问于 2016-09-29 10:55:29
回答 2查看 265关注 0票数 1

我一直在尝试基于信息的不同的变化,但还没有找到一个可行的解决方案。

我试图在单元测试中注入一个服务以进行测试。

我目前的实现如下。问题是,服务不是:ReferenceError: Can't find variable: SerializationService --我已经尝试过在每个服务之前使用inject(),但都没有效果。

spec.js

代码语言:javascript
复制
'use strict';

describe('SerializationService', function () {

  var SerializationService;

  beforeEach(function () {

    module('app');

    inject(function (_SerializationService_) {
      SerializationService = _SerializationService_;
    });
  });

  it('should have a SerializationService', function () {
    expect(SerializationService).toBeDefined();
  });
});

我尝试过的其他事情:

  • 使用单独的模块包括在app模块之间,并使用下划线表示法在另一个前端中注入服务。

karma.conf

代码语言:javascript
复制
// Karma configuration
// http://karma-runner.github.io/0.12/config/configuration-file.html
// Generated on 2016-08-15 using
// generator-karma 0.8.3

module.exports = function (config) {
  'use strict';

  config.set({
    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,

    // base path, that will be used to resolve files and exclude
    basePath: '../../',

    // testing framework to use (jasmine/mocha/qunit/...)
    frameworks: ['jasmine'],

    // list of files / patterns to load in the browser
    files: [
      'bower_components/angular/angular.js',
      'bower_components/angular-mocks/angular-mocks.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/json3/lib/json3.js',
      'bower_components/es5-shim/es5-shim.js',
      'bower_components/jquery/dist/jquery.js',
      'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap.js',
      'bower_components/angular-resource/angular-resource.js',
      'bower_components/angular-cookies/angular-cookies.js',
      'bower_components/angular-sanitize/angular-sanitize.js',
      'bower_components/angular-animate/angular-animate.js',
      'bower_components/angular-touch/angular-touch.js',
      'bower_components/angular-route/angular-route.js',
      'bower_components/angular-ui-router/release/angular-ui-router.js',
      'bower_components/angular-audio/app/angular.audio.js',
      'bower_components/socket.io.client/dist/socket.io-1.3.5.js',
      'bower_components/angular-socket-io/socket.js',
      'bower_components/angular-local-storage/dist/angular-local-storage.js',
      'bower_components/moment/moment.js',
      'bower_components/angular-moment/angular-moment.js',
      'bower_components/angular-scroll-glue/src/scrollglue.js',
      'bower_components/angular-gravatar/build/angular-gravatar.js',
      'bower_components/angular-socialshare/dist/angular-socialshare.min.js',
      'bower_components/clipboard/dist/clipboard.js',
      'bower_components/ngclipboard/dist/ngclipboard.js',
      'bower_components/color-thief/src/color-thief.js',
      'bower_components/ngColorThief/angular-colorthief.js',
      'bower_components/hashids/dist/hashids.js',
      'bower_components/ngEmbed/src/ng-embed.js',
      'bower_components/angular-bootstrap/ui-bootstrap-tpls.js',
      'bower_components/ngEmbed/src/ng-embed.js',
      'app/scripts/**/*.js',
      'test/unit/mock/**/*.js',
      'test/unit/spec/**/*.js'
    ],

    // list of files / patterns to exclude
    exclude: [],

    // web server port
    port: 8081,

    // Start these browsers, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera
    // - Safari (only Mac)
    // - PhantomJS
    // - IE (only Windows)
    browsers: [
      'PhantomJS'
    ],

    // Which plugins to enable
    plugins: [
      'karma-phantomjs-launcher',
      'karma-jasmine'
    ],

    // Continuous Integration mode
    // if true, it capture browsers, run tests and exit
    singleRun: false,

    colors: true,

    // level of logging
    // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
    logLevel: config.LOG_INFO

    //// Uncomment the following lines if you are using grunt's server to run the tests
    // proxies: {
    //   '/': 'http://localhost:9000/'
    // },
    // //URL root prevent conflicts with the site root
    // urlRoot: '_karma_'
  });
};
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-01 16:09:27

问题在我的模拟模块中。

我使用的是板条箱方法,而不是get:

这是错误的:

代码语言:javascript
复制
angular
  .module('meow-mock', [])
  .factory('somemock', function () {
  //code
  });

这是对的:

代码语言:javascript
复制
angular
  .module('meow-mock')
  .factory('somemock', function () {
  //code
  });
票数 0
EN

Stack Overflow用户

发布于 2016-09-29 14:40:56

我在柱塞上创建了一个小示例:柱塞实例

它使用角度模拟和茉莉花进行测试。主要部分:

代码语言:javascript
复制
describe('TestService', function(){
  beforeEach(module('test'));

  var TestService;
  beforeEach(inject(function(_TestService_) {
    TestService = _TestService_;
  }));

  it('returns 42', function(){
    expect(TestService.testMethod()).toEqual(42);
  });
});

您必须使用inject,它是用角模拟定义的。它注入服务、控制器等。下划线被自动去除,它只是语法糖,可以将您的变量命名为TestService

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39768351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档