Share
Embed
Jasmine - The JavaScript BDD Testing
Joseph Chiang
September 06, 2013
Transcript
124 slides
-
• What is Software Testing? • Jasmine • Writing Good
Tests • Matchers in depth • More Jasmine Features • Spies • Using Jasmine with Other tools • What’s it like? ݤἒӑἱ֥ᇶⅳ -
• What is Software Testing? • Jasmine • Writing Good
Tests • Matchers in depth • More Jasmine Features • Spies • Using Jasmine with Other tools • What’s it like? ݤἒӑἱ֥ᇶⅳ ⦁೮ਔđ∉൞ିᾣ؟ഒĤ -
cBad -Ớ5%%#%%֥ࢺℚaೂޅለ⇔ݺ֥ 5FTU٤ӈֹᾏb cGood - ồᆀޓॹỚ+BTNJOFഈ൭ğ c.BUDIFS∻4QJFTᾣ֤⃔↾ၞb cႵଧུ+BTNJOF۽ऎॖၛႨb ಯಖ൞၂ЧݺℼĆ Practical
> Theoretical ၩຓ൬♽ğၹ㬪ޓ؟ၐ↜đሱّ࠭طᅳ֞۷؟⊷ਘa⇥֞ޓ؟b -
Jasmine is a behavior-driven development framework for testing JavaScript code.
What is BDD? ṦႨᆀwྛ㬪xĤ۵5%%ཌྷّĤ -
Jasmine is a behavior-driven development framework for testing JavaScript code.
What is BDD? ṦႨᆀwྛ㬪xĤ۵5%%ཌྷّĤ -
What is BDD? ༵⇔4QFDᄜℯቔ 3%֥࢘؇ Automation Testing ҆౷ 檢查 trim('
hello') 的回傳值是否為 'hello' http://assertselenium.com/2012/11/05/difference-between-tdd-bdd-atdd/ -
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/ -
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/ -
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/ -
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/ -
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/ -
#%%ᆺ൞Ṧℷ֥⇔ቔἀ۬ 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 -
#%%ᆺ൞Ṧℷ֥⇔ቔἀ۬ 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 -
#%%ᆺ൞Ṧℷ֥⇔ቔἀ۬ 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 -
Getting to Know +BTNJOF֥ӈᾖႨم • Syntax ࠎẒ∽م • Matchers ᾋҰ≁௹ݔ
• DOM Support? ∻)5.-ἦ๙ -
Getting to Know +BTNJOF֥ӈᾖႨم • Syntax ࠎẒ∽م • Matchers ᾋҰ≁௹ݔ
• DOM Support? ∻)5.-ἦ๙ • loadFixture -
Getting to Know +BTNJOF֥ӈᾖႨم • Syntax ࠎẒ∽م • Matchers ᾋҰ≁௹ݔ
• DOM Support? ∻)5.-ἦ๙ • loadFixture • Spies ڿṉࠇᾄℶ࠻Ⴕٚمࠇࡱ -
Getting to Know +BTNJOF֥ӈᾖႨم • Syntax ࠎẒ∽م • Matchers ᾋҰ≁௹ݔ
• DOM Support? ∻)5.-ἦ๙ • loadFixture • Spies ڿṉࠇᾄℶ࠻Ⴕٚمࠇࡱ • Async ٤҄ẕ -
Syntax describe 'Trim' ჭࡱẀaॖ/FTUFE str = null beforeEach -> str
= ' Hello World' afterEach -> str = null ૄἠ4QFD⊋ྛభᗥ֥ọቔ -
Syntax describe 'Trim' it 'should trim the leading whitespace', ->
ჭࡱẀaॖ/FTUFE ἲ۬䪌ૼ str = null beforeEach -> str = ' Hello World' afterEach -> str = null ૄἠ4QFD⊋ྛభᗥ֥ọቔ -
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⊋ྛభᗥ֥ọቔ -
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⊋ྛభᗥ֥ọቔ -
Matchers • toEqual • toBe • toBeTruthy • toBeFalsy •
toContain • toBeDefined • toBeUndefined • toBeNull • toBeNaN • toBeGreaterThan • toBeLessThan • toBeCloseTo • toMatch • toThrow ⇼⊈߭ẖᆴ൞ڎᆞ↥↪ -
Matchers • toEqual • toBe • toBeTruthy • toBeFalsy •
toContain • toBeDefined • toBeUndefined • toBeNull • toBeNaN • toBeGreaterThan • toBeLessThan • toBeCloseTo • toMatch • toThrow 可與 not ձႨđ২ೂ expect(‘xxx’).not.toContain(‘y’) ⇼⊈߭ẖᆴ൞ڎᆞ↥↪ -
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ğ -
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ğ -
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.֥Ҡቔđ≾สࡱṦℷ⃦⅌؟ਔĆ -
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: -
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: -
it 'should have .btn-save class', -> loadFixture('button') expect($('.btn')).toHaveClass('btn-save') Matchers loadFixture
(in jasmine-jquery) ॖϜ෮ླ֥)5.-ቓӮỒ৫֥'JYUVSFẵσđေႨℭᄜၛՎٚم≘ೆ -
it 'should have .btn-save class', -> loadFixture('button') expect($('.btn')).toHaveClass('btn-save') Matchers loadFixture
(in jasmine-jquery) ॖϜ෮ླ֥)5.-ቓӮỒ৫֥'JYUVSFẵσđေႨℭᄜၛՎٚم≘ೆ ಀ≘ೆGJYUVSFTCVUUPOIUNM ॖ܂Ṧℷaቋᾏၞ֥)5.- -
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 ࣼॖᆰࢤᄝՎ౼֤Ć -
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.-ἧ -
Spies ᾄ॥ଖἠٚم൞ڎФ⊋ྛ foo = getMsg: (name = 'World') -> "Hello
#{name}!" say: -> msg = @.getMsg('Spy') alert msg ೂޅTBZႵ⊋ྛHFU.TH -
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 -
Spies Ấڿ߭ẖᆴ spyOn(Camera, 'checkRecording').andReturn(null) BOE3FUVSO Useful for Buggy API ૼૼႵ૫a႗൞ေڿ㬪㢻૫
ằṦℷᇏႵႨ֞Ⴕ↜ⅳֻ֥ٚӱൔa Ⴛ҂མቓ.PDLđ≾ӑݺႨ -
Spies Ấڿ߭ẖᆴ spyOn(Camera, 'checkRecording').andReturn(null) BOE3FUVSO Useful for Buggy API ૼૼႵ૫a႗൞ေڿ㬪㢻૫
ằṦℷᇏႵႨ֞Ⴕ↜ⅳֻ֥ٚӱൔa Ⴛ҂མቓ.PDLđ≾ӑݺႨ -
Spies Ấڿ߭ẖᆴ spyOn(Camera, 'checkRecording').andReturn(null) BOE3FUVSO Useful for Buggy API ૼૼႵ૫a႗൞ေڿ㬪㢻૫
ằṦℷᇏႵႨ֞Ⴕ↜ⅳֻ֥ٚӱൔa Ⴛ҂མቓ.PDLđ≾ӑݺႨ ὕႵBOE$BMM'BLF KBTNJOFDSFBUF4QZ KBTNJOFDSFBU4QZ'VODUJPO -
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() -
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() -
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֥ -
jasminerice All In One Package for Rails! Ἵཐ Integrate Asset
Pipeline + jasmine-jquery 3BJMTῘứᆀ㢻Ⴕ῾֥ࢶ१đ၂ἠ(FNյປ൬۽Ć https://github.com/bradphelan/jasminerice -
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/