我如何测试 AngularJS 提供者? [英] How can i test a AngularJS provider?

查看:17
本文介绍了我如何测试 AngularJS 提供者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测试我自己的 Angular 提供程序,我需要在配置和运行阶段都对其进行测试,以检查配置方法是否有效,以及实例化的提供程序是否确实配置了正确的参数.

当我询问提供者的依赖注入时,它找不到 APIResourceFactoryProvider,只有 APIResourceFactory,而且我在迄今为止查看的存储库中没有找到任何这样的示例.

解决方案

这实际上比在 AngularJS 中测试提供程序要简单得多:

describe('Testing a provider', function() {var 提供者;beforeEach(module('plunker', function(myServiceProvider) {提供者 = 我的服务提供者;}));it('在方法调用时应该返回 true', inject(function () {期望(提供者.method()).toBeTruthy();}));});

```

证明在 Plunker:http://plnkr.co/edit/UkltiSG8sW7ICb9YBZSHp>

I need to test my own angular provider, and I need to test it in both config and run phase to check that config methods work and that the instantiated provider is indeed configured with the correct parameters.

When I ask dependancy injection for the provider, it can't find the APIResourceFactoryProvider, only the APIResourceFactory, and I haven't found any examples of this on the repositories I've looked trough so far.

解决方案

It's actually a lot simpler than it would at first seem to test a provider in AngularJS:

describe('Testing a provider', function() {
  var provider;

  beforeEach(module('plunker', function( myServiceProvider ) {
      provider = myServiceProvider;
  }));

  it('should return true on method call', inject(function () {
    expect( provider.method() ).toBeTruthy();
  }));
});

```

The proof is in the Plunker: http://plnkr.co/edit/UkltiSG8sW7ICb9YBZSH

这篇关于我如何测试 AngularJS 提供者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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