I'm using the following code to mock the dependecy with authservice:
login.component.spec
import { LoginComponent } from "./login.component";
import { ComponentFixture, inject, TestBed } from "@angular/core/testing";
import { async } from "q";
import { MatCardModule } from "@angular/material";
import { AuthService } from "../../services/auth/auth.service";
import { Log } from "@angular/core/testing/src/logger";
import { NO_ERRORS_SCHEMA } from "@angular/core";
class MockAuthService extends AuthService {
isAuthenticated() {
return "Mocked";
}
}
describe("LoginComponent", () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
let componentService: AuthService;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [LoginComponent],
providers: [AuthService],
imports: [MatCardModule]
});
TestBed.overrideComponent(LoginComponent, {
set: { providers: [{ provide: AuthService, useClass: MockAuthService }] }
});
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
componentService = fixture.debugElement.injector.get(AuthService);
}));
it("Service injected via component should be and instance of MockAuthService", () => {
expect(componentService instanceof MockAuthService).toBeTruthy();
});
});
login.component
import {Component, OnInit} from '@angular/core';
import {AuthService} from '../../services/auth/auth.service';
import {Router} from '@angular/router';
import {GithubService} from '../../services/github/github.service';
import {Errorcode} from './errorcode.enum';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.sass'],
})
export class LoginComponent implements OnInit {
public loginError: string | boolean = false;
constructor(public authService: AuthService, public router: Router, private data: GithubService) {
}
public signInWithGithub(): void {
this.authService.loginwithGithubProvider()
.then(this.loginError = null)
.catch(err => {
if (err === Errorcode.FIREBASE_POPUP_CLOSED) {
this.loginError = 'The popup has been closed before authentication';
}
if (err === Errorcode.FIREBASE_REQUEST_EXESS) {
this.loginError = 'To many requests to the server';
}
}
);
}
public logout(): void {
this.authService.logout();
}
ngOnInit() {
}
}
But if i take a look at the results i keep getting the following error:
Error: StaticInjectorError(DynamicTestModule)[AuthService -> AngularFireAuth]: StaticInjectorError(Platform: core)[AuthService -> AngularFireAuth]: NullInjectorError: No provider for AngularFireAuth! in http://localhost:9876/_karma_webpack_/vendor.js (line 59376)
Any idea's how i might solve this?