- JasmineJS 教程
- JasmineJS - 主页
- JasmineJS - 概述
- JasmineJS - 环境设置
- JasmineJS - 编写文本和执行
- JasmineJS - BDD 架构
- JasmineJS - 测试构建块
- JasmineJS - 匹配器
- JasmineJS - 跳过块
- JasmineJS - 平等检查
- JasmineJS - 布尔检查
- JasmineJS - 顺序检查
- JasmineJS - 空检查
- JasmineJS - 不平等检查
- JasmineJS - 不是数字检查
- JasmineJS - 异常检查
- JasmineJS - beforeEach()
- JasmineJS - afterEach()
- JasmineJS - 间谍
- JasmineJS 有用资源
- JasmineJS - 快速指南
- JasmineJS - 有用的资源
- JasmineJS - 讨论
JasmineJS - 布尔检查
除了相等性检查之外,Jasmine 还提供了一些检查布尔条件的方法。以下是帮助我们检查布尔条件的方法。
ToBeTruthy()
Jasmine 中使用此布尔匹配器来检查结果是否等于 true 或 false。
下面的例子将帮助我们理解toBeTruthy()函数的工作原理。
ExpectSpec.js
describe("Different Methods of Expect Block",function () {
it("The Example of toBeTruthy() method",function () {
expect(expectexam.exampleoftrueFalse(5)).toBeTruthy();
});
});
Expectexam.js
window.expectexam = {
exampleoftrueFalse: function (num) {
if(num < 10)
return true;
else
return false;
},
};
当我们传递小于 10 的数字 5 时,该测试用例将通过并给出以下输出。
如果我们传递一个大于 10 的数字,那么这个绿色测试将变为红色。在第二个屏幕截图中,您可以看到在传递一些大于 10 的值时,预期的测试用例失败并生成红色输出,指出“预期 false 为 true”。
toBeFalsy()
toBeFalsy() 的工作方式也与 toBeTruthy() 方法相同。它将输出匹配为 false,而 toBeTruthy 将输出匹配为 true。下面的例子将帮助您理解toBeFalsy()的基本工作原理。
ExpectSpec.js
describe("Different Methods of Expect Block",function() {
it("The Example of toBeTruthy() method",function () {
expect(expectexam.exampleoftrueFalse(15)).toBeFalsy();
});
});
Expectexam.js
window.expectexam = {
exampleoftrueFalse: function (num) {
if(num < 10)
Return true;
else
return false;
},
};
上面的代码将通过 Jasmine 测试用例,因为我们传递的值超过 10 并且预计输出为 false。因此,浏览器将向我们显示一个绿色标志,这意味着它已经通过。
