【发布时间】:2021-04-07 07:03:28
【问题描述】:
我有一个AuthenticationService,它公开了一个 get 属性:
@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
private currentUserSubject: BehaviorSubject<AuthenticatedUserModel>;
get currentUserValue(): AuthenticatedUserModel {
return this.currentUserSubject.value;
}
}
此服务用于我创建的自定义管道中。为了验证管道是否按预期工作,我试图编写一些测试,但我正在努力模拟 get 属性。我发现这个Stackoverflow post 很有帮助。我尝试了以下所有选项来模拟 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);
我提到的 Stackoverflow 帖子中的一个答案指出这是一个类型问题。我认为将对象称为 spy 可以解决此问题,但事实并非如此。
希望你们能帮助我或为我指明正确的方向。
【问题讨论】:
标签: angular jasmine karma-runner