【发布时间】:2020-09-05 20:15:45
【问题描述】:
我正在尝试测试绑定到内联属性而不是服务上的方法的可观察对象,但是我似乎找不到任何关于如何在 SpyObj 本身或通过spyOn/SpyOnProperty 调用。
// ContentService
readonly content$ = this.http.get('www.testContent.com');
// ParentService
constructor(private readonly contentService: ContentService){
this.loadContent();
}
// This could return different content dependent on locale for example
loadContent(){
this.contentService.content$.subscribe((response)=>{
// Handle response
})
}
// Unit test
let service: ParentService;
let contentServiceSpy: jasmine.SpyObj<ContentService>;
beforeEach(() => {
const spy = jasmine.createSpyObj('ContentService', [], {
// Returns cannot read subscribe of undefined
content$: of()
});
TestBed.configureTestingModule({
providers: [
ParentService,
{ provide: ContentService, useValue: spy }
]
});
service = TestBed.get(ParentService);
contentServiceSpy = TestBed.get(ContentService) as jasmine.SpyObj<ContentService>;
});
我已经看到了一些关于将 spyOnProperty 与 get 和 set 方法一起使用的示例,但这不是这里发生的情况,我是否在 jasmine.SpyObj 上错误地声明了它,或者是否有我缺少返回的特定 jasmine 方法content$ 中的所需值
【问题讨论】:
-
不应该有
{ provide: ContentService, useValue: { content$: spy() } }而不是ValueService吗?据我所知,父母注入ContentService
标签: javascript angular typescript jasmine