注射模拟角度服务的依存关系 [英] Injecting mock angular service dependencies

查看:86
本文介绍了注射模拟角度服务的依存关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务,输入,在模块定义的看跌期权,即依赖于第二个服务,InputCreator。我需要存根InputCreator服务,以测试输入服务。

I have a service, 'Inputs', defined in module 'Puts', that depends on a second service, 'InputCreator'. I need to stub the InputCreator service in order to test the Inputs service.

据我了解这里回答,我要创建一个包含我的存根服务模块,然后创建一个新的测试模块,指定被测模块,然后存根模块依赖关系。再从拉动注射器的服务。像这样:

As I understand the answer here, I should create a module containing my stub service, then create a new 'Test' module, specifying the module under test and then the stub module as dependencies. And then pull the service from the injector. Like so:

beforeEach(function() {

  angular.module.('Puts'); // contains the service 'Inputs'

  angular.module('Mocks',[])
    .service('InputCreator',function(){
      var mockInputs = {
        //stubbed behaviour goes here
      };
      return mockInputs;
    });
  });

  angular.module('Test',['Puts', 'Mocks'];

  inject(function($injector){
    Inputs = $injector.get('Inputs');
  });
});

不过,喷油器功能与'; - 输入未知InputsProvider&LT'响应。

However, the injector function responds with 'unknown InputsProvider <- Inputs'.

我在哪里误入歧途?

谢谢!

推荐答案

已经想通了这一点,我想我会回答我的问题。上面的大错误是使用angular.module而非angular.mock.module,由角模拟模块引用是方便。他们根本不是一回事!

Having figured this out, I thought I'd answer my own question. The big mistake above was using angular.module rather than angular.mock.module, that is convenience referenced as module by angular-mock. They aren't the same thing at all!

此外,它足以初始化angular.mock.module模拟服务,只要你这样做,你初始化被测模块之前。没有必要为这个包裹模块第三个模块中的业务如上面链接的问题建议。即:

Additionally, it's enough to initialize the mock service with angular.mock.module, so long as you do it before you initialize the module under test. There's no need for this 'wrapping the modules in a third module' business as suggested in the question linked above. To wit:

describe("Test Service", function() {
  var TestService, getvaluestub;

  beforeEach(function() {

    // create mock service
    var mock = {getvalue:function(){}}

    angular.module('dependencymodule',[])
      .service('dependencyservice',function () {
        return mock;
      });

    //mock the function we are stubbing, (that, in this case, returns value 4)
    getvaluestub = sinon.stub(mock,'getvalue')returns(4);

    //instantiate your mock service
    module('dependencymodule');

    //instantiate the module of the service under test, 
    //that depends on 'dependencyservice' mocked above 
    //(ie - testmodule includes the     service 'testservice')
    module('testmodule');

    //inject your test service for testing
    inject(function ($injector) {
        TestService = $injector.get('testservice');
    })

  //tests go here.....

如果依赖模块已经存在,您既可以仍然这样做上述所有的,或者你可以获取来自$喷油器的服务,将您的间谍和存根和>然后&LT;被测实例化的服务。重要的是,间谍/存根都设置>前&LT;依赖服务被实例化,否则将没有他们被实例化。它看起来是这样的:

If the dependency module already exists, you could either still do all of the above, or you could acquire the service from the $injector, insert your spies and stubs, and >then< instantiate the service under test. It's important that the spies/stubs are set up >before< the dependent service is instantiated, or it will be instantiated without them. It looks like this:

describe("Test Service", function() {
  var TestService, DependencyService, getvaluestub;

  beforeEach(function() {

    // these modules are specified in the application
    module('dependencymodule');
    module('testmodule');

    inject(function ($injector) {
      DependencyService = $injector.get('testservice');

      getvaluestub = sinon.stub(DependencyService,'getvalue').returns(4);

      OtherService = $injector.get('otherservice');
    })
  });

// test go here

所以,你去那里。但愿这是谁的人对注射进嘲笑角服务'搜索有用的。

So, there you go. Hopefully this is useful to someone who searches for 'Injecting mocks into angular services'.

这篇关于注射模拟角度服务的依存关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆