Jasmine 如何用茉莉花业力覆盖函数的所有行
如何使用jasmine覆盖下面函数的所有行Jasmine 如何用茉莉花业力覆盖函数的所有行,jasmine,karma-jasmine,karma-coverage,jasmine2.0,Jasmine,Karma Jasmine,Karma Coverage,Jasmine2.0,如何使用jasmine覆盖下面函数的所有行 addUser(): void { if (this.validateNewUser()) { this.newUser._Job = this.selectedJob; this.newUser.PositionId = this.selectedJob.Id; this.newUser.Position = this.selectedJob.Value; this.n
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();
});
编辑
现在:
如果显式调用被测方法(
addUser
),则无需检查该方法是否已被调用。但是,您应该检查该方法是否执行了它应该执行的操作。您可能想知道是否展示了祝酒词。因此,您可以如下重写测试
it('#addUser should display toast', () => {
// given
spyOn(toastService, 'open');
// when
component.addUser();
// then
expect(toastService.open).toHaveBeenCalled();
});
很酷,它在某些行中有效,但不是所有行都有效,因为我可以涵盖所有行,我发送了问题中的照片显然
this.validateNewUser()
返回false
,由您来找出原因。您能帮我完成另一个返回承诺的函数吗?@Guilherme Prado
:请为新发行的版本发布一个新问题。@uminder,我在这里发布了