如何使用jasmine覆盖下面函数的所有行?
addUser(): void {
if (this.validateNewUser()) {
this.newUser._Job = this.selectedJob;
this.newUser.PositionId = this.selectedJob.Id;
this.newUser.Position = this.selectedJob.Value;
this.newUser._Area = this.selectedArea;
this.newUser.AreaId = this.selectedArea.Id;
this.newUser.Area = this.selectedArea.Value;
this.users.push(this.newUser);
this.clear();
this.toastService.open('Usuário incluído com sucesso!', { type: 'success', close: true });
}
}我目前正在尝试如下操作,但没有考虑覆盖任何行:
it('Given_addUser_When_UserStepIsCalled_Then_ExpectToBeCalled', (done) => {
component.addUser = jasmine.createSpy();
component.addUser();
expect(component.addUser).toHaveBeenCalled();
done();
});编辑过的
现在:Image here
发布于 2019-12-11 20:46:17
如果显式调用被测方法(addUser),则无需检查该方法是否已被调用。但是,您应该检查该方法是否做了它应该做的事情。您可能想知道是否显示了吐司。因此,您可以按如下方式重写测试。
it('#addUser should display toast', () => {
// given
spyOn(toastService, 'open');
// when
component.addUser();
// then
expect(toastService.open).toHaveBeenCalled();
});https://stackoverflow.com/questions/59285198
复制相似问题