Reputation: 9229
I am trying to test the following angular (4.1) service:
@Injectable()
export class JobService {
private answerSource = new Subject<AnswerWrapper>();
answer$ = this.answerSource.asObservable();
answer(answer: AnswerWrapper) {
this.answerSource.next(answer);
}
which I am testing with this unit-test:
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))
}));
However, I get the error:
Cannot read property 'subscribe' of undefined
I really can't see why it would be undefined, as it should be setup on service creation?
Upvotes: 0
Views: 23
Reputation: 20471
you need to put this line in a constructor
answer$ = this.answerSource.asObservable();
The answerSource Subject is guaranteed to be initialised before the constructor is run
Upvotes: 1