When I try to implement a test like this:
import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { SharedModule } from '../shared/shared.module';
import { AuthInterface, AuthService, Credentials } from '../core/auth/index';
import { LoginComponent } from './login.component';
let comp: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
let de: DebugElement;
let el: HTMLElement;
//////// SPECS /////////////
describe('Login component ', function() {
const AuthServiceStub: AuthInterface = {
isLoggedIn: true,
redirectUrl: '/home',
isAuthTokenStored(): boolean {
return false;
},
isAuthTokenExpired(): boolean {
return false;
},
login(input: Credentials): Observable<boolean> {
return Observable.of(true);
},
logout(): void { }
};
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
SharedModule
],
declarations: [LoginComponent],
providers: [{ provide: AuthService, useValue: AuthServiceStub }]
}).compileComponents().then(() => {
fixture = TestBed.createComponent(LoginComponent);
comp = fixture.componentInstance;
});
}));
describe('Basic test: ', function() {
it('should instantiate a LoginComponent', async(() => {
expect(comp instanceof LoginComponent).toBe(true, 'should create LoginComponent');
}));
});
});
The one and only test present it this test suite fails and I get the following error:
Error: No provider for AuthService!
The problem with this component is that it has an external HTML template, thus I have to use the async callback function in the beforeEach instruction in order to call the TestBed.compileComponents() method.
On the other side, this same test suite succeds if I use an inline template and I replace async with a regular anonymous function.
Aucun commentaire:
Enregistrer un commentaire