JasmineJS — afterEach()



與 beforeEach() 一樣,afterEach() 的工作方式完全相同。它在執行 spec 塊後執行。讓我們使用以下程式碼修改上一個示例。

var currentVal = 0; 

afterEach(function() { 
   currentVal = 5;  
});  

describe("Different Methods of Expect Block",function() { 
   it("first call ", function() { 
      expect(currentVal).toEqual(0);     
   });     
   
   it("second call ",  function() { 
      expect(currentVal).toEqual(5);     
   });
});

在上述示例中,在執行第一個 spec 塊時,currentVal 的值為 0。因此,它將透過測試用例,但執行第一個 it 塊後,Jasmine 編譯運行了 afterEach() 塊,這導致 currentVal 的值變為 5。因此,它也滿足第二個用例,並輸出綠屏。

AfterEach
廣告