在_servicename_中的下划线是什么意思在AngularJS测试?

前端之家收集整理的这篇文章主要介绍了在_servicename_中的下划线是什么意思在AngularJS测试?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在以下示例测试中,原始提供程序名称为APIEndpointProvider,但是对于注入和服务实例化,约定似乎是必须使用下划线来包装它。这是为什么?
  1. 'use strict';
  2.  
  3. describe('Provider: APIEndpointProvider',function () {
  4.  
  5. beforeEach(module('myApp.providers'));
  6.  
  7. var APIEndpointProvider;
  8. beforeEach(inject(function(_APIEndpointProvider_) {
  9. APIEndpointProvider = _APIEndpointProvider_;
  10. }));
  11.  
  12. it('should do something',function () {
  13. expect(!!APIEndpointProvider).toBe(true);
  14. });
  15.  
  16. });

什么是约定我缺少一个更好的解释?

下划线是一个方便的技巧,我们可以使用不同的名称注入一个服务,这样我们可以在本地分配一个与服务同名的局部变量。

也就是说,如果我们不能这样做,我们必须在本地使用一些其他名称作为服务:

  1. beforeEach(inject(function(APIEndpointProvider) {
  2. AEP = APIEndpointProvider; // <-- we can't use the same name!
  3. }));
  4.  
  5. it('should do something',function () {
  6. expect(!!AEP).toBe(true); // <-- this is more confusing
  7. });

在测试中使用的$ injector可以删除下划线,给我们想要的模块。它不做任何事情,除了让我们重用相同的名称

Read more in the Angular docs

猜你在找的Angularjs相关文章