Angularテスト2-service

1777 ワード

テストサービス


テストサービスは最も簡単で、以下の例は基本的なテスト方法と非同期テスト方法を含む.
import { TestBed, inject } from '@angular/core/testing';

import { InjectServiceService } from './inject-service.service';

fdescribe('InjectServiceService', () => {
  console.log(InjectServiceService);
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [InjectServiceService]
    });
  });

  it('should be created', inject([InjectServiceService], (service: InjectServiceService) => {
    console.log(service);
    expect(service).toBeTruthy();
  }));
  it('normal function test',  inject([InjectServiceService], (service: InjectServiceService) => {
    expect(service.add(5)).toEqual(6,'dddddddddddddddddddd');
  }));
  it('return promise function', inject([InjectServiceService], async (service: InjectServiceService) => {
    let getreturn = await service.asyncFunc();
    expect(getreturn).toEqual('getReturn', 'sdddddddddddddddddddddddss');
  }));
});

注入依存の問題をどのように解決するか


依存注入は,deps:[],useClassなど,前述の依存注入の方法と同様の注入方式でよい.