首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用jasmine-karma覆盖函数的所有行

如何用jasmine-karma覆盖函数的所有行
EN

Stack Overflow用户
提问于 2019-12-11 19:51:33
回答 1查看 207关注 0票数 0

如何使用jasmine覆盖下面函数的所有行?

代码语言:javascript
运行
复制
   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 });
    }
}

我目前正在尝试如下操作,但没有考虑覆盖任何行:

代码语言:javascript
运行
复制
    it('Given_addUser_When_UserStepIsCalled_Then_ExpectToBeCalled', (done) => {
        component.addUser = jasmine.createSpy();           
        component.addUser();
        expect(component.addUser).toHaveBeenCalled();
        done();
    });

编辑过的

现在:Image here

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-11 20:46:17

如果显式调用被测方法(addUser),则无需检查该方法是否已被调用。但是,您应该检查该方法是否做了它应该做的事情。您可能想知道是否显示了吐司。因此,您可以按如下方式重写测试。

代码语言:javascript
运行
复制
it('#addUser should display toast', () => {

    // given
    spyOn(toastService, 'open');

    // when
    component.addUser();

    // then
    expect(toastService.open).toHaveBeenCalled();
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59285198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档