
- JasmineJS 教程
- JasmineJS - 首頁
- JasmineJS - 概述
- JasmineJS - 環境搭建
- JasmineJS - 書寫測試用例及執行
- JasmineJS - BDD 架構
- JasmineJS - 測試的構建塊
- JasmineJS - Matchers
- JasmineJS - 跳過塊
- JasmineJS - 等式檢查
- JasmineJS - 布林值檢查
- JasmineJS - 順序檢查
- JasmineJS - 空值檢查
- JasmineJS - 不等式檢查
- JasmineJS - 非數字檢查
- JasmineJS - 異常檢查
- JasmineJS - beforeEach()
- JasmineJS - afterEach()
- JasmineJS - Spies
- JasmineJS 有用資源
- JasmineJS - 快速指南
- JasmineJS - 有用資源
- JasmineJS - 討論
JasmineJS - 不等式檢查
到目前為止,我們已經討論了 Jasmine 中不同的方法,這些方法可以幫助我們根據需求測試不同的場景。在本章中,我們將學習不同的 matchers,它們將幫助我們檢查 JS 檔案中的不等式條件。以下是為此目的使用的 matchers。
ToBeGreaterThan()
顧名思義,此 matcher 用於檢查大於條件。讓我們使用以下程式碼段修改我們的 **customerMatcher.js**。
describe("Different Methods of Expect Block",function () { var exp = 8; it("Example of toBeGreaterThan()", function () { expect(exp).toBeGreaterThan(5); }); });
在上面的程式碼段中,我們期望變數 **“exp”** 的值大於 5。現在,由於變數“exp”的值為“8”,大於“5”,這段程式碼將生成綠色截圖。

現在,讓我們再次將變數的值修改為“4”,並使此測試失敗。為此,我們需要使用以下程式碼段修改 **js** 檔案。
describe("Different Methods of Expect Block",function () { var exp = 4; it ("Example of toBeGreaterThan()", function () { expect(exp).toBeGreaterThan(5); }); });
此程式碼將失敗,因為值 4 不能大於 5。因此,它將產生以下輸出。

ToBeLessThan()
此 matcher 用於檢查測試場景的小於條件。它的行為與 toBeGreaterThan() matcher 正好相反。現在讓我們看看此 matcher 如何工作。讓我們相應地修改 **customerMatcher.js** 檔案。
describe("Different Methodsof Expect Block",function () { var exp = 4; it("Example of toBeLessThan()", function() { expect(exp).toBeLessThan(5); }); });
與之前的示例一樣,我們有一個值為“4”的變數。在這段程式碼中,我們檢查此變數的值是否小於 5。這段程式碼將生成以下輸出。

現在,為了使其失敗,我們需要為變數 exp 賦值一些更大的數字。讓我們這樣做並測試應用程式。我們將為 **exp** 賦值 25,這肯定會出現錯誤,併產生以下紅色截圖。

廣告