是什么在_servicename_下划线在AngularJS测试意味着什么呢? [英] What does the underscores in _servicename_ mean in AngularJS tests?

查看:357
本文介绍了是什么在_servicename_下划线在AngularJS测试意味着什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的例子中测试,原来提供者的名字是APIEndpointProvider,但对于注射和服务的实例化的公约似乎是它必须用下划线包裹它被注入。这是为什么?

In the following example test, the original provider name is APIEndpointProvider, but for injection and service instantiation the convention seems to be it has to be injected with underscores wrapping it. Why is that?

'use strict';

describe('Provider: APIEndpointProvider', function () {

  beforeEach(module('myApp.providers'));

  var APIEndpointProvider;
  beforeEach(inject(function(_APIEndpointProvider_) {
    APIEndpointProvider = _APIEndpointProvider_;
  }));

  it('should do something', function () {
    expect(!!APIEndpointProvider).toBe(true);
  });

});

什么是我缺少一个更好的解释惯例?

What is the convention I'm missing a better explanation to?

推荐答案

的下划线是一个方便的技巧,我们可以用它来注入下一个不同名称的服务,使我们可以在本地分配相同名称的局部变量作为服务

The underscores are a convenience trick we can use to inject a service under a different name so that we can locally assign a local variable of the same name as the service.

也就是说,如果我们不能做到这一点,我们不得不使用一些其他的名字本地服务:

That is, if we couldn't do this, we'd have to use some other name for a service locally:

beforeEach(inject(function(APIEndpointProvider) {
  AEP = APIEndpointProvider; // <-- we can't use the same name!
}));

it('should do something', function () {
  expect(!!AEP).toBe(true);  // <-- this is more confusing
});

$注射器用于测试能够只是删除下划线给我们我们想要的模块。它没有的的任何东西,除了让我们重复使用相同的名称。

The $injector used in testing is able to just remove the underscores to give us the module we want. It doesn't do anything except let us reuse the same name.

<一个href=\"https://docs.angularjs.org/api/ngMock/function/angular.mock.inject#resolving-references-underscore-wrapping-\"相对=nofollow>详情请阅读角文档

这篇关于是什么在_servicename_下划线在AngularJS测试意味着什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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