_servicename_ 中的下划线在 AngularJS 测试中是什么意思? [英] What does the underscores in _servicename_ mean in AngularJS tests?

查看:34
本文介绍了_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
});

测试中使用的 $injector 能够只删除下划线以提供我们想要的模块.除了让我们重用相同的名称外,它不会任何事情.

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.

在 Angular 中阅读更多内容文档

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

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