如何测试 Jasmine 中未声明的变量?
How to test for undeclared variable in Jasmine?
it("is undefined", function() {
expect(total).toBe(undefined);
});
此规范失败:ReferenceError: total is not defined
如何测试一个变量没有声明?
jasmine 中的测试与检查普通未定义变量相同:
(typeof total === 'undefined')
您可以将它与 jasmine 一起使用,例如:
expect(typeof total).toBe('undefined');
typeof
operator returns 一个字符串,如果未定义其操作数,则具有不抛出的特殊行为。
请注意 jasmine does have toBeDefined
matchers,当您可以安全地访问对象时(通常如果它是一个成员并且您知道父对象存在),这是首选。根据您的测试设置方式,如果您感兴趣的变量是 this
或 window
或其他范围的成员,您可以使用这些匹配器:
expect(this.total).toBeDefined();
如果你可以使用这些,你应该。
it("is undefined", function() {
expect(total).toBe(undefined);
});
此规范失败:ReferenceError: total is not defined
如何测试一个变量没有声明?
jasmine 中的测试与检查普通未定义变量相同:
(typeof total === 'undefined')
您可以将它与 jasmine 一起使用,例如:
expect(typeof total).toBe('undefined');
typeof
operator returns 一个字符串,如果未定义其操作数,则具有不抛出的特殊行为。
请注意 jasmine does have toBeDefined
matchers,当您可以安全地访问对象时(通常如果它是一个成员并且您知道父对象存在),这是首选。根据您的测试设置方式,如果您感兴趣的变量是 this
或 window
或其他范围的成员,您可以使用这些匹配器:
expect(this.total).toBeDefined();
如果你可以使用这些,你应该。