2012-05-12 12 views
44

मैं परीक्षण लिखने के लिए जैस्मीन का उपयोग कर रहा हूं।जैस्मीन में वैश्विक 'पहले से पहले'?

मेरे पास कई परीक्षण फ़ाइलें हैं, प्रत्येक फ़ाइल में beforeEach है, लेकिन वे बिल्कुल वही हैं।

मैं वैश्विकbeforeEach उनके लिए कैसे प्रदान करूं?

उत्तर

7

आप इसे अपने spec_helper.js फ़ाइल में डाल सकते हैं और इसे ठीक काम करना चाहिए।

+0

इस 'spec_helper.js' चमेली द्वारा स्वचालित रूप से पहचाना जाएगा करता है? – Freewind

+0

स्वचालित रूप से नहीं - हमने 'helpers' निर्देशिका में हमारा जोड़ा। – x1a4

+1

क्या आपका मतलब है, हमें अपनी प्रत्येक टेस्ट फाइलों में '(' ./ spec_helper ') की आवश्यकता होनी चाहिए? – Freewind

57

x1a4 के उत्तर ने मुझे भ्रमित कर दिया। यह और अधिक स्पष्ट हो सकता है:

जब आप बाहर सभी describe ब्लॉक एक beforeEach समारोह घोषित, यह प्रत्येक परीक्षा से पहले ट्रिगर किया जाएगा (ताकि प्रत्येक it से पहले)। इससे कोई फर्क नहीं पड़ता कि आप अपने describe ब्लॉक से पहले या उसके बाद beforeEach घोषित करते हैं।

It's not mentioned in the documentation.

// Example: 

beforeEach(function() { 
    localStorage.clear(); 
}); 

describe('My tests', function() { 
    describe('Test localstorage', function() { 

     it('Adds an item to localStorage', function() { 
      localStorage.setItem('foo', 'bar'); 
      expect(localStorage.getItem('foo')).toBe('bar'); 
     }); 

     it('Is now empty because our beforeEach cleared localStorage', function() { 
      expect(localStorage.getItem('foo')).toBe(null); 
     }); 

    }); 
}); 
संबंधित मुद्दे