NullInjectorError: No provider for AnimationBuilder! jasmin karma test failure
NullInjectorError: No provider for AnimationBuilder! jasmin karma test failure
我正在研究 angular 11。我对 jasmin/karma 测试还很陌生。我为我的应用程序创建了启动画面。但是当 运行 ng 测试时,我收到 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.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 但没有成功。我怎样才能消除这个错误?
我会存根 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();
});
我正在研究 angular 11。我对 jasmin/karma 测试还很陌生。我为我的应用程序创建了启动画面。但是当 运行 ng 测试时,我收到 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.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 但没有成功。我怎样才能消除这个错误?
我会存根 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();
});