I am writing test cases for angular2 components.
I had created a service which uses observable stream as below:
import {Injectable} from '@angular/core'
import {Subject} from 'rxjs/Subject';
import {User} from './user.model';
@Injectable()
export class UserService {
selectedUserInstance:User = new User();
// Observable selectedUser source
private selectedUserSource = new Subject<User>();
// Observable selectColumn stream
selectedUser$ = this.selectedUserSource.asObservable();
// service command
selectUser(user:User) {
this.selectedUserInstance=user;
this.selectedUserSource.next(user);
}
}
Now In my component I have subscribed to this stream as :
getSelectedUser() {
this.subscriptionUser = this.userService.selectedUser$.subscribe(
selectedUser => {
this.selectedUser = selectedUser;
}
);
}
Now in my spec.ts file, I want to mock this stream as :
spyOn(userService, 'selectedUser$')
.and.returnValue(Observable.of({
'name': 'bhushan',
'desc': 'student'
}));
But it keeps giving me following error:
Error: spyOn could not find an object to spy upon for selectColumn$()
is there any way to do this?
I am stuck on this issue for very long time now.
any inputs?
thanks