JasmineJS-テストのビルディングブロック

この章では、ジャスミンによるテストの構成要素について説明します。

スイートブロック

JasmineはJavaScriptのテストフレームワークです。 SuiteJasmineフレームワークの基本的な構成要素です。特定のファイルまたは関数用に作成された同様のタイプのテストケースのコレクションは、1つのスイートと呼ばれます。他に2つのブロックが含まれています。1つは“Describe()” もう1つは “It()”

1つのSuiteブロックには、2つのパラメーターのみを含めることができます。 “name of that suite” と別の “Function declaration” これは、実際にテスト対象のユニット機能を呼び出します。

次の例では、ユニットテストの追加関数を追加するスイートを作成します。 add.jsファイル。この例では、JSファイルの名前が“calculator.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); 
   }); 
});

上記の関数では、2つの関数を宣言しました。関数add その関数と別の関数に引数として指定された2つの数値を追加します addAny 引数として指定された数値を追加する必要があります。

このファイルを作成したら、このファイルをに追加する必要があります “SpecRunner.html”ヘッドセクションの内側。コンパイルが成功すると、結果として次の出力が生成されます。

ネストされたスイートブロック

スイートブロックは、別のスイートブロック内に多くのスイートブロックを持つことができます。次の例は、別のスイートブロック内に別のスイートブロックを作成する方法を示しています。2つのJavaScriptファイルを作成します。1つは次の名前です。“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;    
   }  
};

上記のコードは、実行の結果として次の出力を生成します specRunner.html このファイルをheadセクション内に追加した後のファイル。

ブロックの説明

前に説明したように、describeブロックはSuiteブロックの一部です。Suiteブロックと同様に、2つのパラメーターが含まれています。“the name of the describe block” と別の “function declaration”。今後の例では、Jasmineスイートブロックの動作フローを理解するために、多くのdescribeブロックを実行します。以下は、完全なdescribeブロックの例です。

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 1つの中のブロック describe ブロック。

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必要な関数またはJavaScriptファイルから期待値を書き込むことができます。それは下に来るITブロック。1つのITブロックに複数のExpectブロックを含めることができます。

以下はExpectブロックの例です。このexpectブロックは、JavaScript関数またはJavaScriptファイルを単体テストするためのさまざまなメソッドを提供します。各Expectブロックは、matcher。マッチャーには2つの異なるタイプがあります。1つはinbuilt matcher と別の user defined matchers

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ブロックのさまざまな組み込みメソッドのさまざまな使用法について説明します。