My current task involves testing an Angular (4.1) service that looks like this:
@Injectable()
export class JobService {
private answerSource = new Subject<AnswerWrapper>();
answer$ = this.answerSource.asObservable();
answer(answer: AnswerWrapper) {
this.answerSource.next(answer);
}
I am using the following unit test to validate the service:
beforeEach(() => {
TestBed.configureTestingModule({
providers: [JobService]
});
});
it('should emit $answer on answer()', inject([JobService], (service: JobService) => {
service.answer$.subscribe(val => expect(val).toEqual('test123'));
service.answer(('test123' as any))
}));
Despite my efforts, I encountered the error message:
Cannot read property 'subscribe' of undefined
I am puzzled by this because I believe everything should be properly set up during service creation. Any insights on why this could be happening?