我在 Angular 13 作業,我正在使用 ngRxstore。這是我的代碼:
userState:UserState | null = null;
constructor(private store:Store<any>) {}
ngOnInit() {
// Subscribe to the user state and save info in the localstorage
this.store.subscribe((state) => {
this.userState = state.appUserState;
if(this.userState && this.userState.user){
localStorage.setItem('userState', btoa(JSON.stringify(this.userState.user.roles)) );
}
});
}
誰能指導我如何在 ngOninit 中對我的商店訂閱進行單元測驗。這是我的測驗,但它不起作用:
const testStore = jasmine.createSpyObj('Store', ['subscribe']);
//....
providers: [
//..
{
provide: Store,
useValue: testStore
}
]
//...
it(`#ngOninit test'`, () => {
testStore.subscribe.and.returnValue(
of({ user: userDtoMock, errorMessage:'',dataState:UserStateEnum.LOADED })
);
fixture.detectChanges();
homeComponent.ngOnInit();
});
提前致謝。
uj5u.com熱心網友回復:
這是總體思路,請根據您的需要進行調整:
// Mock properly
const someState = {};
const testStore = of(someState);
//....
providers: [
{
provide: Store,
useValue: testStore
}
]
//...
it(`#ngOninit test'`, (done) => { // Add a callback function for async testing
// Subscribe to your state
testStore.subscribe(state => {
// Check your test
expect(...).toXXX(...);
// Call the callback
done();
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/480873.html
上一篇:漢堡選單中的滑動添加水平滾動條
