【问题标题】:NullInjectorError: No provider for AnimationBuilder! jasmin karma test failureNullInjectorError:AnimationBuilder 没有提供者!茉莉业力测试失败
【发布时间】:2021-05-19 20:51:09
【问题描述】:

我正在研究 Angular 11。我对 jasmin/karma 测试非常陌生。我为我的应用程序创建了启动画面。但是在运行 ng test 时我收到 appComponent 说的错误

Chrome 90.0.4430.212 (Windows 10) AppComponent should create the app appComponent FAILED
    NullInjectorError: R3InjectorError(DynamicTestModule)[SplashScreenService -> AnimationBuilder -> AnimationBuilder]:
      NullInjectorError: No provider for AnimationBuilder!
    error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ 'SplashScreenService', 'AnimationBuilder', 'AnimationBuilder' ] })
    NullInjectorError: R3InjectorError(DynamicTestModule)[SplashScreenService -> AnimationBuilder -> AnimationBuilder]:
      NullInjectorError: No provider for AnimationBuilder!
        at NullInjector.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11116:1)
        at R3Injector.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11283:1)
        at R3Injector.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11283:1)
        at injectInjectorOnly (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:4776:1)
        at ɵɵinject (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:4780:1)
        at Object.SplashScreenService_Factory [as factory] (ng:///SplashScreenService/ɵfac.js:4:47)
        at R3Injector.hydrate (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11452:1)
        at R3Injector.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11272:1)
        at NgModuleRef$1.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:25337:1)
        at Object.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:25051:1)

app.component.ts

    import { Component } from '@angular/core';
    import { SplashScreenService } from './main/services/splash-screen.service';

    @Component({
       selector: 'app-root',
       templateUrl: './app.component.html',
       styleUrls: ['./app.component.scss']
    })
    export class AppComponent {

    constructor(private splashScreenService:  SplashScreenService) { }
  }

app.component.spec.ts

it('should create the app appComponent', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });

splash-screen.service.ts

import { animate, AnimationBuilder, AnimationPlayer, style } from '@angular/animations';
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { filter, take } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class SplashScreenService {

  splashScreenEl: any;
  player: AnimationPlayer;

  constructor(private animationBuilder: AnimationBuilder,
    @Inject(DOCUMENT) private document: any,
    private router: Router) {
    this.init();
  }

  init() {
    // Get the splash screen element
    this.splashScreenEl = this.document.body.querySelector('#splash-screen');

    // If the splash screen element exists...
    if (this.splashScreenEl) {
      // Hide it on the first NavigationEnd event
      this.router.events.pipe(filter((event => event instanceof NavigationEnd)), take(1)).subscribe(() => {
        setTimeout(() => {
          this.hide();
        });
      });
    }
  }

  hide() {
    this.player = this.animationBuilder.build([
      style({ opacity: '1' }),
      animate('400ms ease', style({
        opacity: '0',
        zIndex: '-10'
      }))
    ]).create(this.splashScreenEl);

    setTimeout(() => {
      this.player.play();
    }, 0);
  }
}

splash-screen.service.spec.ts

import { TestBed } from '@angular/core/testing';
import { SplashScreenService } from './splash-screen.service';

describe('SplashScreenService', () => {
  let service: SplashScreenService;

  beforeEach(() => {
    TestBed.configureTestingModule({});
    service = TestBed.inject(SplashScreenService);
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });
});

我尝试在 Testbed 中注入 AnimationBuilder,但没有成功。如何消除此错误?

【问题讨论】:

    标签: angular typescript unit-testing karma-jasmine


    【解决方案1】:

    我会存根SplashScreenService

    beforeEach(waitForAsync(() => {
        TestBed.configureTestingModule({
         declarations: [AppComponent],
         // add this line. We are saying when you want SplashScreenService, you get {}
         providers: [{ provide: SplashScreenService, useValue: {} }],
        }).compileComponents();
      }));
    
    it('should create the app appComponent', () => {
        const fixture = TestBed.createComponent(AppComponent);
        const app = fixture.componentInstance;
        expect(app).toBeTruthy();
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-23
      • 2023-04-06
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      相关资源
      最近更新 更多