JasmineJS教程

JasmineJS 测试构建块

在本章中,我们将讨论 Jasmine 的测试构建块。

套房块

Jasmine 是一个 JavaScript 测试框架。 Suite 是 Jasmine 框架的基本构建块。为特定文件或函数编写的类似类型测试用例的集合称为一个套件。它包含另外两个块,一个是 "Describe()",另一个是 "It()"
一个 Suite 块只能有两个参数,一个 "该套件的名称" 和另一个 "函数声明" 实际上调用我们的单元功能待测试。
在以下示例中,我们将创建一个套件,该套件将对 add.js 文件中的添加功能进行单元测试。在这个例子中,我们有一个名为 "calculator.js" 的 JS 文件,它将通过 Jasmine 进行测试,对应的 Jasmine 规范文件是 "CalCulatorSpec.js"

Calculator.js

window.Calculator = { 
   
   currentVal:0,  
   varAfterEachExmaple:0, 
   
   add:function (num1) { 
      this.currentVal += num1; 
      return this.currentVal;    
   },     
   
   addAny:function () {    
      var sum = this.currentVal; 
    
      for(var i = 0; i < arguments.length; i++) { 
         sum += arguments[i]; 
      } 
      
      this.currentVal = sum; 
      return  this.currentVal; 
   }, 
};

CalculatorSpec.js

describe("calculator",function() { 
   
   //test case: 1  
   it("Should retain the current value of all time", function () {
      expect(Calculator.currentVal).toBeDefined();
      expect(Calculator.currentVal).toEqual(0);  
   }); 
   
   //test case: 2  
   it("should add numbers",function() {
      expect(Calculator.add(5)).toEqual(5); 
      expect(Calculator.add(5)).toEqual(10);  
   });         
    
   //test case :3   
   it("Should add any number of numbers",function () {
      expect(Calculator.addAny(1,2,3)).toEqual(6); 
   }); 
}); 
在上面的函数中,我们声明了两个函数。函数 add 会将作为参数给出的两个数字相加到该函数,另一个函数 addAny 应该将作为参数给出的任何数字相加。
创建此文件后,我们需要将此文件添加到 head 部分的 "SpecRunner.html" 中。编译成功后,将生成以下输出结果。
Calculatorspec

Nested Suites Block

套件块可以在另一个套件块中包含多个套件块。以下示例将向您展示如何在另一个套件块中创建不同的套件块。我们将创建两个 JavaScript 文件,一个名为 "NestedSpec.js",另一个名为 "nested.js"

NestedSpec.js

describe("nested",function() { 
   
   // Starting of first suite block  
   // First block    
  
   describe("Retaining values ",function () {
   
      //test case:1    
      it ("Should retain the current value of all time", function () { 
         expect(nested.currentVal).toBeDefined();   
         expect(nested.currentVal).toEqual(0);   
      });    
   }); //end of the suite block   
   //second suite block 
   describe("Adding single number ",function () {     
   
      //test case:2 
      it("should add numbers",function() { 
         expect(nested.add(5)).toEqual(5); 
         expect(nested.add(5)).toEqual(10); 
      });         
   }); //end of the suite block  
   //third suite block 
   describe("Adding Different Numbers",function () {  
   
      //test case:3 
      it("Should add any number of numbers",function() {  
         expect(nested.addAny(1,2,3)).toEqual(6);  
      });    
   }); //end of the suite block 
});

Nested.js

window.nested = { 
   
   currentVal: 0,
  
   add:function (num1) {  
      this.currentVal += num1;     
      return this.currentVal;    
   },
   
   addAny:function () { 
      var sum = this.currentVal; 
    
      for(var i = 0;i < arguments.length; i++) { 
         sum += arguments[i]; 
      } 
    
      this.currentVal = sum; 
      return this.currentVal;    
   }  
};
在 head 部分中添加此文件后,运行 specRunner.html 文件后,上述代码将生成以下输出。
SpecRunner 结果

描述块

如前所述,describe 块是 Suite 块的一部分。与 Suite 块一样,它包含两个参数,一个 "描述块的名称" 和另一个 "函数声明"。在接下来的示例中,我们将通过许多描述块来了解 Jasmine 套件块的工作流程。以下是完整描述块的示例。
describe("Adding single number ",function () { 
   
   it("should add numbers",function() { 
      expect(nested.add(5)).toEqual(5); 
      expect(nested.add(5)).toEqual(10); 
   });     
}

IT 块

像描述块一样,我们也被引入了 IT 块。它位于描述块内。这是实际包含每个单元测试用例的块。在下面的代码中,一个 describe 块内有多个 IT 块。
describe("Adding single number ",function () { 
   
   // test case : 1   
   it("should add numbers",function() {  
      expect(nested.add(5)).toEqual(5); 
      expect(nested.add(5)).toEqual(10); 
   });         
    
   //test case : 2 
   it("should add numbers",function() { 
      expect(nested.addAny(1,2,3)).toEqual(6); 
   });     
}

期望块

Jasmine Expect 允许您从所需的函数或 JavaScript 文件中编写您的期望。它属于 IT 块。一个 IT 块可以有多个 Expect 块。
以下是 Expect 块的示例。这个 expect 块提供了多种方法来单元测试您的 JavaScript 函数或 JavaScript 文件。每个 Expect 块也称为 matcher。有两种不同类型的匹配器,一种是 内置匹配器,另一种是 用户定义的匹配器
describe("Adding single number ",function () {   
   
   // test case : 1 
   it("should add numbers",function() {
      expect(nested.add(5)).toEqual(5); 
      expect(nested.add(5)).toEqual(10);
   });          
   
   //test case : 2 
   it("should add numbers",function() {
      expect(nested.addAny(1,2,3)).toEqual(6); 
   });     
}
在接下来的章节中,我们将讨论 Expect 块的不同内置方法的各种用途。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4