I have a service with a replay subject.
export class UserService {
public userChanged: ReplaySubject<User> = new ReplaySubject<User>();
...
public getUser(userId?): void {
...
this.http.get(url, httpOptions).pipe(
catchError(this.handleError('getUser', null, 'Couldn\'t get user', options))
).subscribe( (user: User) => {
this.userChanged.next(user);
});
}
My component subscribes to userChanged
.
this.userService.userChanged.subscribe((user) => {
this.user = user;
});
Now, I want to mock my UserService
in the component test:
1 option Testing Observables in Angular)
import { of } from 'rxjs';
...
const userServiceSpy = jasmine.createSpyObj('UserService', {'userChanged': of({_id: '1'}) });
or 2 option)
const userServiceSpy = jasmine.createSpyObj('UserService', {'userChanged': () => of({_id: '1'}) });
or 3 option angular test tutorial)
const userServiceSpy = jasmine.createSpyObj('UserService', ['userChanged']});
const userChangedSpy = userServiceSpy.userChanged.and.returnValue( of({_id: '1'}) );
+
TestBed.configureTestingModule({
...
providers: [
...
{provide: UserService, useValue: userServiceSpy}
],
schemas: [NO_ERRORS_SCHEMA]
})
give me this err:
this.userService.userChanged.subscribe is not a function
Shouldn't of
return an Observable to subscribe to?
Question: How to mock this?