I am currently working with Angular 7 and Angular CLI version 7.3.4. I have a component that is being unit tested which involves injecting 2 services. Below, you will find the essential code snippets including the stub causing errors, the spec file, and the crucial part of the component in question. Despite my efforts, I keep encountering an error "TypeError: this.spreading.getSpreadingDays is not a function." I have already injected the service, so the function should be defined. I believe it's a simple mistake on my end, but even after referencing various tutorials, I can't seem to identify what's wrong with my code. Any advice or suggestions would be greatly appreciated.
Spec file
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { SpreadSignUpComponent } from './spread-sign-up.component';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { AuthService } from 'src/app/core/services/auth.service';
import { SpreadingService } from 'src/app/core/services/spreading.service';
import { AuthServiceStub } from 'src/app/core/testing/auth.service.stub';
import { SpreadingServiceStub } from 'src/app/core/testing/spreading.service.stub';
describe('SpreadSignUpComponent', () => {
let component: SpreadSignUpComponent;
let fixture: ComponentFixture<SpreadSignUpComponent>;
let authService: AuthService;
let spreadingService: SpreadingService;
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
{ provide: AuthService, useValue: AuthServiceStub },
{ provide: SpreadingService, useValue: SpreadingServiceStub }
],
declarations: [ SpreadSignUpComponent ],
schemas: [ NO_ERRORS_SCHEMA ],
})
.compileComponents();
authService = TestBed.get(AuthService);
spreadingService = TestBed.get(SpreadingService);
}));
beforeEach(() => {
fixture = TestBed.createComponent(SpreadSignUpComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
stub class:
import { SpreadingService } from '../services/spreading.service';
import { of } from 'rxjs';
import { SpreadingDay } from 'src/app/shared/models/spreadingDay';
import { I } from 'src/app/shared/testing/interfaceBuilder';
export class SpreadingServiceStub implements I<SpreadingService> {
getSpreadingDays() {
return of([]);
}
getSpreadingDaysForUser() {
return of([]);
}
setSpreadingDay(
spreadingDay: SpreadingDay
) {
return <Promise<void>>{};
}
addVolunteerToDay(
spreadingDay: SpreadingDay,
userID: string,
) {
return <Promise<void>>{};
}
removeVolunteerFromDay(
spreadingDay: SpreadingDay,
userID: string,
) {
return <Promise<void>>{};
}
addRequestToDay(
spreadingDay: SpreadingDay,
userID: string,
first: string,
last: string,
) {
return <Promise<void>>{};
}
}
component:
import { Component, OnInit } from '@angular/core';
import { SpreadingService } from 'src/app/core/services/spreading.service';
import { SpreadingDayReturn, SpreadingDay } from 'src/app/shared/models/spreadingDay';
import { Observable } from 'rxjs';
import { AuthService } from 'src/app/core/services/auth.service';
@Component({
selector: 'app-spread-sign-up',
templateUrl: './spread-sign-up.component.html',
styleUrls: ['./spread-sign-up.component.scss']
})
export class SpreadSignUpComponent implements OnInit {
spreadingDays$: Observable<SpreadingDayReturn[]>;
constructor(
private auth: AuthService,
private spreading: SpreadingService,
) { }
ngOnInit() {
this.spreadingDays$ = this.spreading.getSpreadingDays();
}