Jasmine 建议代码覆盖率的角度单元测试
请建议对上述代码进行单元测试。太好了,先生。解释得很好。我会用这个来投票,很好,先生。解释得很好。我将使用这个,并将投票表决这个答案。Jasmine 建议代码覆盖率的角度单元测试,jasmine,angular-unit-test,Jasmine,Angular Unit Test,请建议对上述代码进行单元测试。太好了,先生。解释得很好。我会用这个来投票,很好,先生。解释得很好。我将使用这个,并将投票表决这个答案。 async ngOnInit() { if (localStorage.getItem('Authorization') && this.formdataservice.get_account_user_id()) { try { let res: any = await this.formdataservic
async ngOnInit() {
if (localStorage.getItem('Authorization') && this.formdataservice.get_account_user_id()) {
try {
let res: any = await this.formdataservice.get_preferences().toPromise();
const { data } = res.body;
this.formdataservice.set_preferences(data);
} catch (e) {}
}
}
it('should call set_preferences if Authorization is set in local storage and acount_user_id is populated', async(done) => {
// arrange => you will have to set Authorization in local storage before the first detect changes in the beforeEach block because that's when `ngOnInit` is called. Also, mock formDataService.get_Acount_user_id() to return a truthy value;
// also have to mock `get_preferences()` to return a promise for res;
// wait for promises to finish
await fixture.whenStable(); // this waits for promises to finish
// assert => expect `set_preferences()` toHaveBeenCalledWith whatever data is.
done();
});