Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Jasmine - The JavaScript BDD Testing

Joseph Chiang
September 06, 2013

Jasmine - The JavaScript BDD Testing

Joseph Chiang

September 06, 2013
Tweet

More Decks by Joseph Chiang

Other Decks in Technology

Transcript

  1. • What is Software Testing? • Jasmine • Writing Good

    Tests • Matchers in depth • More Jasmine Features • Spies • Using Jasmine with Other tools • What’s it like? ݤἒӑἱ֥ᇶⅳ
  2. • What is Software Testing? • Jasmine • Writing Good

    Tests • Matchers in depth • More Jasmine Features • Spies • Using Jasmine with Other tools • What’s it like? ݤἒӑἱ֥ᇶⅳ ⦁೮ਔđ∉൞ିᾣ؟ഒĤ
  3. What is BDD? ༵⇔4QFDᄜℯቔ 3%֥࢘؇ Automation Testing ೘҆౷ 檢查 trim('

    hello') 的回傳值是否為 'hello' http://assertselenium.com/2012/11/05/difference-between-tdd-bdd-atdd/
  4. What is BDD? ༵⇔4QFDᄜℯቔ 3%֥࢘؇ ၂∄༵⇔4QFDᄜℯቔđ֌࢘؇∻∽م҂๝ č္⇝൞4ZTUFN"OBMZ[FS֥࢘؇Ĥ Automation Testing ೘҆౷

    檢查 trim(' hello') 的回傳值是否為 'hello' http://assertselenium.com/2012/11/05/difference-between-tdd-bdd-atdd/
  5. What is BDD? ༵⇔4QFDᄜℯቔ 3%֥࢘؇ ၂∄༵⇔4QFDᄜℯቔđ֌࢘؇∻∽م҂๝ č္⇝൞4ZTUFN"OBMZ[FS֥࢘؇Ĥ Automation Testing ೘҆౷

    檢查 trim(' hello') 的回傳值是否為 'hello' trim ⽅方法應該刪除左側的空⽩白 http://assertselenium.com/2012/11/05/difference-between-tdd-bdd-atdd/
  6. What is BDD? ༵⇔4QFDᄜℯቔ 3%֥࢘؇ ၂∄༵⇔4QFDᄜℯቔđ֌࢘؇∻∽م҂๝ č္⇝൞4ZTUFN"OBMZ[FS֥࢘؇Ĥ Automation Testing ೘҆౷

    5%%∻#%%ᄝެ֥൞ӱൔ⁣ὰᆞ⃷ "5%%≣൞ᄝެ㸗௖ℯቔᆞ⃷ 檢查 trim(' hello') 的回傳值是否為 'hello' trim ⽅方法應該刪除左側的空⽩白 http://assertselenium.com/2012/11/05/difference-between-tdd-bdd-atdd/
  7. What is BDD? ༵⇔4QFDᄜℯቔ 3%֥࢘؇ ၂∄༵⇔4QFDᄜℯቔđ֌࢘؇∻∽م҂๝ č္⇝൞4ZTUFN"OBMZ[FS֥࢘؇Ĥ Automation Testing ೘҆౷

    5%%∻#%%ᄝެ֥൞ӱൔ⁣ὰᆞ⃷ "5%%≣൞ᄝެ㸗௖ℯቔᆞ⃷ 檢查 trim(' hello') 的回傳值是否為 'hello' trim ⽅方法應該刪除左側的空⽩白 使⽤用者輸⼊入的密碼不應該以空⽩白開頭 http://assertselenium.com/2012/11/05/difference-between-tdd-bdd-atdd/
  8. What is BDD? ༵⇔4QFDᄜℯቔ 3%֥࢘؇ ၂∄༵⇔4QFDᄜℯቔđ֌࢘؇∻∽م҂๝ č္⇝൞4ZTUFN"OBMZ[FS֥࢘؇Ĥ Automation Testing ೘҆౷

    5%%∻#%%ᄝެ֥൞ӱൔ⁣ὰᆞ⃷ "5%%≣൞ᄝެ㸗௖ℯቔᆞ⃷ ҉Ṧğ"5%%∣ἐ္൞༵Ⴕ4QFD 檢查 trim(' hello') 的回傳值是否為 'hello' trim ⽅方法應該刪除左側的空⽩白 使⽤用者輸⼊入的密碼不應該以空⽩白開頭 http://assertselenium.com/2012/11/05/difference-between-tdd-bdd-atdd/
  9. #%%ᆺ൞Ṧℷ֥⇔ቔἀ۬ suite = new Y.TestSuite('String Functions Tests') suite.add new Y.Test.Case

    name: 'trim() Tests', testTrimWithLeadingWhiteSpace: -> result = trim(' Hello World') Y.Assert.areEqual('Hello World', result) testTrimWithLeadingWhiteSpace: -> result = trim('Hello World ') Y.Assert.areEqual('Hello World', result) *Unit-Style BDD-Style
  10. #%%ᆺ൞Ṧℷ֥⇔ቔἀ۬ suite = new Y.TestSuite('String Functions Tests') suite.add new Y.Test.Case

    name: 'trim() Tests', testTrimWithLeadingWhiteSpace: -> result = trim(' Hello World') Y.Assert.areEqual('Hello World', result) testTrimWithLeadingWhiteSpace: -> result = trim('Hello World ') Y.Assert.areEqual('Hello World', result) describe 'Trim' it 'should trim the leading whitespace', -> result = trim(' Hello World') expect(result).toEqual('Hello World') it 'should trim the trailing whitespace', -> result = trim('Hello World ') expect(result).toEqual('Hello World') *Unit-Style BDD-Style
  11. #%%ᆺ൞Ṧℷ֥⇔ቔἀ۬ suite = new Y.TestSuite('String Functions Tests') suite.add new Y.Test.Case

    name: 'trim() Tests', testTrimWithLeadingWhiteSpace: -> result = trim(' Hello World') Y.Assert.areEqual('Hello World', result) testTrimWithLeadingWhiteSpace: -> result = trim('Hello World ') Y.Assert.areEqual('Hello World', result) describe 'Trim' it 'should trim the leading whitespace', -> result = trim(' Hello World') expect(result).toEqual('Hello World') it 'should trim the trailing whitespace', -> result = trim('Hello World ') expect(result).toEqual('Hello World') *Unit-Style BDD-Style ڿ೿Ἶ֥aಸၞ倛ồaạ5FTUṉӮ4QFD
  12. Getting to Know +BTNJOF֥ӈᾖႨم • Syntax ࠎẒ∽م • Matchers ᾋҰ≁௹᾵ݔ

    • DOM Support? ∻)5.-ἦ๙ • loadFixture • Spies ڿṉࠇᾄℶ࠻Ⴕٚمࠇ໾ࡱ
  13. Getting to Know +BTNJOF֥ӈᾖႨم • Syntax ࠎẒ∽م • Matchers ᾋҰ≁௹᾵ݔ

    • DOM Support? ∻)5.-ἦ๙ • loadFixture • Spies ڿṉࠇᾄℶ࠻Ⴕٚمࠇ໾ࡱ • Async ٤๝҄ẕ৘
  14. Syntax describe 'Trim' ჭࡱ଀Ẁaॖ/FTUFE str = null beforeEach -> str

    = ' Hello World' afterEach -> str = null ૄἠ4QFD⊋ྛభᗥ֥ọቔ
  15. Syntax describe 'Trim' it 'should trim the leading whitespace', ->

    ჭࡱ଀Ẁaॖ/FTUFE ἲ۬䪌ૼ str = null beforeEach -> str = ' Hello World' afterEach -> str = null ૄἠ4QFD⊋ྛభᗥ֥ọቔ
  16. Syntax result = trim(' Hello World') expect(result).toEqual('Hello World') describe 'Trim'

    it 'should trim the leading whitespace', -> ჭࡱ଀Ẁaॖ/FTUFE ἲ۬䪌ૼ .BUDIFS⃷⃾⊷ਘᆞ⃷∻ڎ str = null beforeEach -> str = ' Hello World' afterEach -> str = null ૄἠ4QFD⊋ྛభᗥ֥ọቔ
  17. Syntax result = trim(' Hello World') expect(result).toEqual('Hello World') describe 'Trim'

    it 'should trim the leading whitespace', -> ჭࡱ଀Ẁaॖ/FTUFE ἲ۬䪌ૼ .BUDIFS⃷⃾⊷ਘᆞ⃷∻ڎ ॖႨ xdescribe ࠣ xit ੻Ἶ str = null beforeEach -> str = ' Hello World' afterEach -> str = null ૄἠ4QFD⊋ྛభᗥ֥ọቔ
  18. Matchers • toEqual • toBe • toBeTruthy • toBeFalsy •

    toContain • toBeDefined • toBeUndefined • toBeNull • toBeNaN • toBeGreaterThan • toBeLessThan • toBeCloseTo • toMatch • toThrow ⇼⊈߭ẖᆴ൞ڎᆞ⃷↥↪
  19. Matchers • toEqual • toBe • toBeTruthy • toBeFalsy •

    toContain • toBeDefined • toBeUndefined • toBeNull • toBeNaN • toBeGreaterThan • toBeLessThan • toBeCloseTo • toMatch • toThrow 可與 not ձ஥൐Ⴈđ২ೂ expect(‘xxx’).not.toContain(‘y’) ⇼⊈߭ẖᆴ൞ڎᆞ⃷↥↪
  20. Matchers DOM Support? it 'should have .btn-save class', -> html

    = '<button class="btn btn-save">Save with Changes</button>' el = document.createElement('div') el.innerHTML = html expect(el.hasClassName('btn-save')).toBeTruthy() +BTNJOFᆺ⊦ᇿṦℷ+BWB4DSJQUaỚᧄ%0.❣↥಩ޅᆦჱb ೂݔ໡ⁿ႗൞ေṦℷa২ೂ$MBTT/BNFğ
  21. Matchers DOM Support? it 'should have .btn-save class', -> html

    = '<button class="btn btn-save">Save with Changes</button>' el = document.createElement('div') el.innerHTML = html expect(el.hasClassName('btn-save')).toBeTruthy() ӑάઐỮĆط౏IBT$MBTT/BNFὕႵ⏟´ఖཌྷಸྟ↜ⅳ +BTNJOFᆺ⊦ᇿṦℷ+BWB4DSJQUaỚᧄ%0.❣↥಩ޅᆦჱb ೂݔ໡ⁿ႗൞ေṦℷa২ೂ$MBTT/BNFğ
  22. it 'should have .btn-save class', -> html = '<button class="btn

    btn-save">Save with Changes</button>' el = document.createElement('div') el.innerHTML = html expect(el.hasClassName('btn-save')).toBeTruthy() Matchers Before: https://github.com/velesin/jasmine-jquery jasmine-jquery భ؊Ṧℷն؟὜⃌ട֞%0.֥Ҡቔđ≾สࡱ⃸Ṧℷ⃦⅌؟ਔĆ
  23. it 'should have .btn-save class', -> html = '<button class="btn

    btn-save">Save with Changes</button>' el = document.createElement('div') el.innerHTML = html expect(el.hasClassName('btn-save')).toBeTruthy() it 'should have .btn-save class', -> btn = $(<button class="btn btn-primary btn-save">Save with Changes</button>') expect(btn).toHaveClass('btn-save') Matchers Before: https://github.com/velesin/jasmine-jquery jasmine-jquery భ؊Ṧℷն؟὜⃌ട֞%0.֥Ҡቔđ≾สࡱ⃸Ṧℷ⃦⅌؟ਔĆ After:
  24. it 'should have .btn-save class', -> html = '<button class="btn

    btn-save">Save with Changes</button>' el = document.createElement('div') el.innerHTML = html expect(el.hasClassName('btn-save')).toBeTruthy() it 'should have .btn-save class', -> btn = $(<button class="btn btn-primary btn-save">Save with Changes</button>') expect(btn).toHaveClass('btn-save') Matchers Before: https://github.com/velesin/jasmine-jquery ิ܂ӑ؟ݺႨ֥%0.ཌྷἬ.BUDIFSTၛࠣ'JYUVSFTᆦჱ jasmine-jquery భ؊Ṧℷն؟὜⃌ട֞%0.֥Ҡቔđ≾สࡱ⃸Ṧℷ⃦⅌؟ਔĆ After:
  25. it 'should have .btn-save class', -> loadFixture('button') expect($('.btn')).toHaveClass('btn-save') Matchers loadFixture

    (in jasmine-jquery) ॖϜ෮ླ֥)5.-ቓӮỒ৫֥'JYUVSFẵσđေႨℭᄜၛՎٚم≘ೆ
  26. it 'should have .btn-save class', -> loadFixture('button') expect($('.btn')).toHaveClass('btn-save') Matchers loadFixture

    (in jasmine-jquery) ॖϜ෮ླ֥)5.-ቓӮỒ৫֥'JYUVSFẵσđေႨℭᄜၛՎٚم≘ೆ ὜ಀ≘ೆGJYUVSFTCVUUPOIUNM ॖ܂Ṧℷaቋᾏၞ֥)5.-
  27. it 'should have .btn-save class', -> loadFixture('button') expect($('.btn')).toHaveClass('btn-save') Matchers loadFixture

    (in jasmine-jquery) ॖϜ෮ླ֥)5.-ቓӮỒ৫֥'JYUVSFẵσđေႨℭᄜၛՎٚم≘ೆ ὜ಀ≘ೆGJYUVSFTCVUUPOIUNM ॖ܂Ṧℷaቋᾏၞ֥)5.- ೏CVUUPOIUNMᇏႵՎDMBTT ࣼॖᆰࢤᄝՎ౼֤Ć
  28. it 'should have .btn-save class', -> loadFixture('button') expect($('.btn')).toHaveClass('btn-save') Matchers loadFixture

    (in jasmine-jquery) ॖϜ෮ླ֥)5.-ቓӮỒ৫֥'JYUVSFẵσđေႨℭᄜၛՎٚم≘ೆ ὜ಀ≘ೆGJYUVSFTCVUUPOIUNM ॖ܂Ṧℷaቋᾏၞ֥)5.- ೏CVUUPOIUNMᇏႵՎDMBTT ࣼॖᆰࢤᄝՎ౼֤Ć ၛభ׻҂ᆩ֡≾ἠỌỌQ ᆇ֥ῒ֤'JYUVSFỚభ؊Ṧℷޓᇗေğॖၛٳ ❣ᇗ♶০Ⴈ)5.-᾵ἧ
  29. Spies ᾄ॥ଖἠٚم൞ڎФ⊋ྛ foo = getMsg: (name = 'World') -> "Hello

    #{name}!" say: -> msg = @.getMsg('Spy') alert msg ೂޅ⃷⃾TBZႵ⊋ྛHFU.TH
  30. Spies ᾄ॥ଖἠٚم൞ڎФ⊋ྛ foo = getMsg: (name = 'World') -> "Hello

    #{name}!" say: -> msg = @.getMsg('Spy') alert msg ೂޅ⃷⃾TBZႵ⊋ྛHFU.TH spyOn(foo, 'getMsg') foo.say() expect(foo.getMsg).toHaveBeenCalled() TQZ0O UP)BWF#FFO$BMMFE
  31. Spies Ấڿ߭ẖᆴ spyOn(Camera, 'checkRecording').andReturn(null) BOE3FUVSO Useful for Buggy API ૼૼႵ὎૫a֌႗൞ေڿ㬪㢻὎૫

    ằṦℷᇏႵႨ֞Ⴕ↜ⅳֻ֥೘ٚӱൔa ֌୆Ⴛ҂མቓ.PDLđ≾ӑݺႨ ὕႵBOE$BMM'BLF KBTNJOFDSFBUF4QZ KBTNJOFDSFBU4QZ'VODUJPO 
  32. Async waitFor, run +BWB4DSJQUႵ⇝؟٤๝֥҄౦㣐ླẕ৘đ২ೂğ൙ࡱaọ὎a≘ೆa⊷ਘẖ℻ # 預設 flag 為 false finished

    = false # 綁定完成後的事件 $form.on('ajax:success') -> finished = true # 製造使⽤用者輸⼊入 $form.find('[name=number]').val('4242424242424242') # 採⽤用 UJS (Rails 的 AJAX) 送出 $form.submit() # 會持續檢查 finished 是否變成 true waitsFor -> finished # 通過 waitFor 才會執⾏行 runs 中的區塊 runs -> expect($form.find('[name=token]').val()).not.toBeFalsy()
  33. Async waitFor, run +BWB4DSJQUႵ⇝؟٤๝֥҄౦㣐ླẕ৘đ২ೂğ൙ࡱaọ὎a≘ೆa⊷ਘẖ℻ # 預設 flag 為 false finished

    = false # 綁定完成後的事件 $form.on('ajax:success') -> finished = true # 製造使⽤用者輸⼊入 $form.find('[name=number]').val('4242424242424242') # 採⽤用 UJS (Rails 的 AJAX) 送出 $form.submit() # 會持續檢查 finished 是否變成 true waitsFor -> finished # 通過 waitFor 才會執⾏行 runs 中的區塊 runs -> expect($form.find('[name=token]').val()).not.toBeFalsy()
  34. Async waitFor, run +BWB4DSJQUႵ⇝؟٤๝֥҄౦㣐ླẕ৘đ২ೂğ൙ࡱaọ὎a≘ೆa⊷ਘẖ℻ # 預設 flag 為 false finished

    = false # 綁定完成後的事件 $form.on('ajax:success') -> finished = true # 製造使⽤用者輸⼊入 $form.find('[name=number]').val('4242424242424242') # 採⽤用 UJS (Rails 的 AJAX) 送出 $form.submit() # 會持續檢查 finished 是否變成 true waitsFor -> finished # 通過 waitFor 才會執⾏行 runs 中的區塊 runs -> expect($form.find('[name=token]').val()).not.toBeFalsy() ∽مЌӻႮഈط༯đὕ⁶4ZOUBY4ZOD֥
  35. jasminerice All In One Package for Rails! ෼Ἵཐ૜ Integrate Asset

    Pipeline + jasmine-jquery 3BJMTῘứᆀ㢻Ⴕ๦῾֥ࢶ१đ၂ἠ(FNյປ൬۽Ć https://github.com/bradphelan/jasminerice
  36. RESOURCES • [PDF] JavaScript Testing with Jasmine http://it-ebooks.info/book/2085/ • [RailsCasts]

    Testing JavaScript with Jasmine (Revised) http://railscasts.com/episodes/261-testing-javascript-with- jasmine-revised • [Slideshare] How BDD style Unit Testing help you write better test code. http://www.slideshare.net/ihower/bdd-style-unit-testing • [Blog] What is TDD, BDD & ATDD ? | Assert Selenium http://assertselenium.com/2012/11/05/difference-between- tdd-bdd-atdd/