angular - 我如何使用 karma + jasmine 测试 Angular 2 中的局部变量?

标签 angular typescript karma-jasmine

我正在尝试用 jasmine 编写单元测试。我面临的问题是如何使用 Jasmine 测试局部变量。我能够测试全局变量但不能测试局部变量。

这是我的功能:

checkDayIsOpen(): void {
  let isSpecial = false;
     this.items.forEach((item) => {
         if (item.is_open) {
             isSpecial = true;
         }
      });
   if(isSpeciality){
       call another function here...
     }
}

我想测试isSpecial的值。

最佳答案

你无法测试它,因为它在调用后就消失了。但是,您可以测试是否调用了其他函数。

describe("Person toString() Test", function() {
  it("calls the getName() function", function() {
    var testPerson = new Person();
    spyOn(testPerson, "getName");
    testPerson.toString();
    expect(testPerson.getName).toHaveBeenCalled();
  });
});

关于angular - 我如何使用 karma + jasmine 测试 Angular 2 中的局部变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49406639/

相关文章:

Angular 2 : Redirect route with parameters and optional parameters

typescript - tsc 编译具有指定模块 ID 的 AMD 模块

javascript - 在 gulp 任务中使用 karma 获取报告

javascript - 是否可以使用 javascript 中的 setAttribute 将属性设置为 [config],值为 configForScroll?

angular - 确定 Bootstrap Accordion 在 Angular 5 中是否打开

c# - 当返回类型为 iActionResult 时,component.ts 类给出错误

typescript - 如何使用 paths 编译器选项使用 tsc 编译特定文件

typescript - 在 typescript 中使用 if 语句删除空检查

javascript - Karma - 测试函数,不返回且不设置范围变量

angular - 自从将 Angular 升级到 2.3.1 后尝试分配给只读属性