1
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) {}
    }
  }

请建议对上述代码进行单元测试。

4

1 回答 1

0
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(); 
});
于 2020-02-21T18:30:10.367 回答