在以下示例测试中,原始提供程序名称为APIEndpointProvider,但是对于注入和服务实例化,约定似乎是必须使用下划线来包装它。这是为什么?
- '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);
- });
- });
什么是约定我缺少一个更好的解释?
下划线是一个方便的技巧,我们可以使用不同的名称注入一个服务,这样我们可以在本地分配一个与服务同名的局部变量。
也就是说,如果我们不能这样做,我们必须在本地使用一些其他名称作为服务:
- 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
- });