[英]angular unit test - How to spyon observable property
我想為 observable 屬性設置發射值/下面是代碼
export class PostListComponent implements OnInit {
errorMessage = '';
loggedInUser: User | undefined;
cachedPost: Post | undefined;
private selectedTabSubject = new BehaviorSubject<number>(0);
selectedTabAction$ = this.selectedTabSubject.asObservable();
selectedTabAction$ 是訂閱控制操作的屬性,即下拉列表更改並返回選定值。
我如何模擬 /spyon 這個屬性,以便我可以在單元測試中控制我想要的值。
在您的單元測試中,在您想要模擬的屬性上創建一個間諜。
...
fixture = TestBed.createComponent(PostListComponent);
component = fixture.componentInstance;
const theValueYouWantToEmit = 3;
spyOnProperty(component, 'selectedTabAction$', 'get').and.returnValue(of(theValueYouWantToEmit));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.