如何使用茉莉花覆盖下面所有功能?
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
答案 0 :(得分:0)
如果明确调用了被测方法(addUser
),则无需检查该方法是否已被调用。但是,您应该检查该方法是否达到了预期的效果。您可能想知道是否显示了吐司。因此,您可以按以下方式重写测试。
it('#addUser should display toast', () => {
// given
spyOn(toastService, 'open');
// when
component.addUser();
// then
expect(toastService.open).toHaveBeenCalled();
});