我正在尝试用 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/