Angular Jasmine使用get属性监视服务

Angular Jasmine使用get属性监视服务,angular,jasmine,karma-runner,Angular,Jasmine,Karma Runner,我有一个公开get属性的AuthenticationService: @Injectable({ providedIn: 'root' }) export class AuthenticationService { private currentUserSubject: BehaviorSubject<AuthenticatedUserModel>; get currentUserValue(): AuthenticatedUserModel { return

我有一个公开get属性的
AuthenticationService

@Injectable({
  providedIn: 'root'
})
export class AuthenticationService {
  private currentUserSubject: BehaviorSubject<AuthenticatedUserModel>;

  get currentUserValue(): AuthenticatedUserModel {
    return this.currentUserSubject.value;
  }
}
@可注入({
providedIn:'根'
})
导出类身份验证服务{
private currentUserSubject:BehaviorSubject非常有用。我尝试了以下所有选项来模拟get属性,但不幸的是到目前为止没有成功

// Property currentUserValue does not have access type get
spyOnProperty(authenticationServiceSpy, 'currentUserValue', 'get').and.returnValue(undefined);

// Cannot read property 'and' of undefined
(Object.getOwnPropertyDescriptor(authenticationServiceSpy, 'currentUserValue')?.get as jasmine.Spy<() => AuthenticatedUserModel>).and.returnValue(undefined);
((Object.getOwnPropertyDescriptor(authenticationServiceSpy, 'currentUserValue')?.get as jasmine.Spy<() => AuthenticatedUserModel>) as jasmine.Spy).and.returnValue(undefined);
//属性currentUserValue没有访问类型get
spyOnProperty(authenticationServiceSpy,'currentUserValue','get')。和.returnValue(未定义);
//无法读取未定义的属性“和”
(Object.getOwnPropertyDescriptor(authenticationServiceSpy,'currentUserValue')?.get as jasmine.Spy AuthenticatedUserModel>)和.returnValue(未定义);
((Object.getOwnPropertyDescriptor(authenticationServiceSpy,'currentUserValue')?.get as jasmine.Spy AuthenticatedUserModel>)as jasmine.Spy)和.returnValue(未定义);
我提到的Stackoverflow帖子中的一个答案指出这是一个类型问题。我认为将对象称为
spy
可以解决这个问题,但事实并非如此


希望你们能帮我或者给我指出正确的方向。

你们应该使用spyOnProperty:

it("allows you to create spies for either type", function() {
  spyOnProperty(someObject, "myValue", "get").and.returnValue(30);
  spyOnProperty(someObject, "myValue", "set").and.callThrough();
});
您可以在此处阅读文档:

不幸的是,我还是犯了同样的错误。我是不是遗漏了什么?