[英]How to spyOn property of a Service that is a BehaviorSubject/Observable?
First of, spyOnProperty
is giving me Property user does not have access type get
with this line: spyOnProperty(userService, 'user', 'get').and.returnValue(Observable.of(adminUser));
首先,
spyOnProperty
给我的Property user does not have access type get
此行: spyOnProperty(userService, 'user', 'get').and.returnValue(Observable.of(adminUser));
I have a UserService with a user
property as such: 我有一个UserService,其
user
属性如下:
export class UserService {
user: BehaviorSubject<User> = new BehaviorSubject(new User());
}
The component that I'm testing needs to change behavior depending on the result of userService.user.subscribe
. 我正在测试的组件需要根据
userService.user.subscribe
的结果更改行为。 Hence, I need to be able to spy on user
. 因此,我需要能够监视
user
。
One idea that I had was to write a method getter, eg getUser()
on class UserService, and don't access user
via a property. 我的一个想法是在类UserService上编写方法getter,例如
getUser()
,而不是通过属性访问user
。
But that seems to be a bit extreme. 但这似乎有点极端。
Any ideas? 有任何想法吗?
您可以使用spyOnProperty返回用户对象的模拟。
spyOnProperty(userService.user, 'value', 'get').and.returnValue({ 'username': 'username'});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.