JasmineJS - 跳過塊



Jasmine 還允許開發人員跳過一個或多個測試用例。這些技術既可以應用於規範級別,也可以應用於套件級別。根據應用的級別,這個塊分別稱為跳過規範跳過套件

在以下示例中,我們將學習如何使用“x”字元跳過特定的規範套件

跳過規範

我們將使用“x”it語句前面修改之前的示例。

describe('This custom matcher example ', function() { 
   
   beforeEach(function() { 
      // We should add custom matched in beforeEach() function. 
      
      jasmine.addMatchers({ 
         validateAge: function() { 
            return { 
               compare: function(actual,expected) { 
                 var result = {}; 
                 result.pass = (actual > = 13 && actual < = 19); 
                 result.message = 'sorry u are not a teen ';  
                 return result; 
               }  
            };   
         }    
      });    
   });  
    
   it('Lets see whether u are teen or not', function() { 
      var myAge = 14; 
      expect(myAge).validateAge();  
   });
   
   xit('Lets see whether u are teen or not ', function() {  
      //Skipping this Spec 
      var yourAge = 18; 
   });
});

如果我們執行這段 JavaScript 程式碼,我們會在瀏覽器中收到以下輸出結果。Jasmine 自身會通知使用者,具體的it塊已被用 “xit” 暫時停用

XIT Block Result

跳過套件

同樣地,我們可以停用 describe 塊,以便實現跳過套件技術。在以下示例中,我們將學習跳過套件塊的過程。

xdescribe('This custom matcher example ', function() {  
   
   //Skipping the entire describe  block  
   beforeEach(function() {  
   
      // We should add custom matched in beforeEach() function.  
      jasmine.addMatchers({  
         validateAge: function() {  
            return {   
               compare: function(actual,expected) {  
                 var result = {}; 
                 result.pass = (actual >=13 && actual<=19); 
                 result.message ='sorry u are not a teen '; 
                 return result;  
               }   
            };   
         }   
      });   
   });

   it('Lets see whether u are teen or not', function() {  
      var myAge = 14; 
      expect(myAge).validateAge(); 
   });  

   it('Lets see whether u are teen or not ', function() {  
      var yourAge = 18; 
      expect(yourAge).validateAge(); 
   });
});

以上程式碼會生成以下螢幕截圖作為輸出。

Skipping Suite

正如我們在訊息欄中看到的那樣,它顯示了兩個處於待定狀態的規範塊,這意味著這兩個規範塊使用“x”字元被停用。在下一章中,我們將討論不同型別的 Jasmine 測試場景。

廣告