填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

createAngularTestingModule

A helper function to use when unit testing Angular services that depend upon upgraded AngularJS services.

檢視"說明"...

      
      createAngularTestingModule(angularJSModules: string[], strictDi?: boolean): Type<any>
    
引數
angularJSModules string[]

a collection of the names of AngularJS modules to include in the configuration.

strictDi boolean

whether the AngularJS injector should have strictDI enabled.

可選. 預設值是 `undefined`.
返回值

Type<any>

說明

This function returns an NgModule decorated class that is configured to wire up the Angular and AngularJS injectors without the need to actually bootstrap a hybrid application. This makes it simpler and faster to unit test services.

Use the returned class as an "import" when configuring the TestBed.

In the following code snippet, we are configuring the TestBed with two imports. The Ng2AppModule is the Angular part of our hybrid application and the ng1AppModule is the AngularJS part.

      
      import {TestBed} from '@angular/core/testing';
import {createAngularJSTestingModule, createAngularTestingModule} from '@angular/upgrade/static/testing';

import {HeroesService, ng1AppModule, Ng2AppModule} from './module';

const {module, inject} = (window as any).angular.mock;

/* . . . */
  beforeEach(() => {
    TestBed.configureTestingModule(
        {imports: [createAngularTestingModule([ng1AppModule.name]), Ng2AppModule]});
  });
    

Once this is done we can get hold of services via the Angular Injector as normal. Services that are (or have dependencies on) an upgraded AngularJS service, will be instantiated as needed by the AngularJS $injector.

In the following code snippet, HeroesService is an Angular service that depends upon an AngularJS service, titleCase.

      
      it('should have access to the HeroesService', () => {
  const heroesService = TestBed.inject(HeroesService);
  expect(heroesService).toBeDefined();
});
    

This helper is for testing services not Components. For Component testing you must still bootstrap a hybrid app. See UpgradeModule or downgradeModule for more information.

The resulting configuration does not wire up AngularJS digests to Zone hooks. It is the responsibility of the test writer to call $rootScope.$apply, as necessary, to trigger AngularJS handlers of async events from Angular.

The helper sets up global variables to hold the shared Angular and AngularJS injectors.

Here is the example application and its unit tests that use createAngularTestingModule and createAngularJSTestingModule.

      
      /**
 * @license
 * Copyright Google LLC All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */

import {TestBed} from '@angular/core/testing';
import {createAngularJSTestingModule, createAngularTestingModule} from '@angular/upgrade/static/testing';

import {HeroesService, ng1AppModule, Ng2AppModule} from './module';

const {module, inject} = (window as any).angular.mock;

describe('HeroesService (from Angular)', () => {
  beforeEach(() => {
    TestBed.configureTestingModule(
        {imports: [createAngularTestingModule([ng1AppModule.name]), Ng2AppModule]});
  });

  it('should have access to the HeroesService', () => {
    const heroesService = TestBed.inject(HeroesService);
    expect(heroesService).toBeDefined();
  });
});


describe('HeroesService (from AngularJS)', () => {
  beforeEach(module(createAngularJSTestingModule([Ng2AppModule])));
  beforeEach(module(ng1AppModule.name));

  it('should have access to the HeroesService', inject((heroesService: HeroesService) => {
       expect(heroesService).toBeDefined();
     }));
});