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 时,此测试用例将通过并为我们提供以下输出。
toBeTruthy 方法
如果我们传递一个大于 10 的数字,那么这个绿色测试将变为红色。在第二个屏幕截图中,您可以看到在传递一些大于 10 的值时,预期的测试用例失败并生成红色输出,指出"预期为真"。
toBeTruthy 错误

toBeFalsy()

toBeFalsy() 的工作方式与 toBeTruthy() 方法相同。它匹配输出为假而 toBeTruthy 匹配输出为真。下面的例子将帮助你理解 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 并且预期输出为假。因此,浏览器会向我们显示一个绿色标志,表示它已通过。
toBeTruthy 方法
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4