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

Unit-Testing With Jasmine

Unit-Testing With Jasmine

第38回 HTML5とか勉強会資料

Yuya Saito

April 26, 2013
Tweet

More Decks by Yuya Saito

Other Decks in Programming

Transcript

  1. AGeNda WHaT Is bDD? f WHaT Is JAsMiNe? , GEt

    MOrE OuT Of JAsMiNe ^ MAkE JAsMiNe mORe USeFul i
  2. g Testability is an inherent quality of good design. You

    can have testability and still have bad design, sure, but you cannot have good design without testability. Think of the tests as small sample use cases - examples of using your code - if testing is hard, it means using the code is hard.
  3. 6 QUeStIoNs On tDD:  ·ͣͲ͔͜Β࢝ΊΕ͹Α͍ͷ͔  ͳʹΛςετ͢Ε͹Α͍ͷ͔  ͳʹΛςετ͢Δඞཁ͕ͳ͍ͷ͔

     ͭͷ΋ͷʹରͯ͠Ͳͷఔ౓ςετ͢Ε͹Α͍ͷ͔  ςετΛͳΜͱݺ΂͹Α͍ͷ͔  ςετ͕ࣦഊͨ͠ཧ༝ΛͲ͏ཧղ͢Ε͹Α͍ͷ͔ v v v v v v v v v v v v v v v v v v v v v v v v v v
  4. DAn NOrTh ANsWeRs: v v v v v v v

    v v v v v v v v v v v v v v v v v v v ͜Ε͔Β࡞੒͠Α͏ͱ͢ΔϓϩάϥϜʹظ଴͞ΕΔ ʮৼΔ෣͍ʯ ΍ ʮ੍໿৚݅ʯ ɺ ͭ·Γ ʮ ཁٻ࢓༷ʯ ʹ ͍ۙܗͰɺ ࣗવݴޠΛซه͠ͳ͕ΒςετίʔυΛ هड़͢Δ͜ͱ͕ղܾ΁ͷۙಓͩͱߟ͑ͨɻ
  5. IN SHoRt: v v v v v v v v

    v v v v v v v v v v v v v v v v v v  ΞϓϦέʔγϣϯશମͰ͸ͳ͘ɺ খ͘͞ɺ ͭͷ͜ͱͷΈςετ͢Δ  ςετΛจষͷΑ͏ʹهड़͢Δ
  6. JAsMiNe Is: v v v v v v v v

    v v v v v v v v v v v v v v v v +BWB4DSJQUͷͨΊͷ ϏϔΠϏΞۦಈͷςετϑϨʔϜϫʔΫ
  7. JAsMiNe Is: v v v v v v v v

    v v v v v v v v v v v v v v v v ͋Δ࢓༷ 4QFD ʹରͯ͠ɺ ίʔυ͕ ͲΜͳৼΔ෣͍Λ͢Δ΂͖͔Λදݱ͢ΔͷΛ खॿ͚ͯ͘͠ΕΔπʔϧ
  8. Suite describe("Hello world", function(){ it("says hello to the world", function(){

    expect(helloWorld().toEqual("Hello world!")); }); });
  9. describe("employee", function(){ var employee; beforeEach(function(){ employee = new Employee; });

    it("has a name", function(){ expect(employee.name.toBeDefined()); }); it("has a role", function(){ expect(employee.role.toBeDefined()); }); });
  10. describe("employee", function(){ var employee; beforeEach(function(){ employee = new Employee; });

    it("has a name", function(){ expect(employee.name.toBeDefined()); }); it("has a role", function(){ expect(employee.role.toBeDefined()); }); });
  11. describe("an async spec", function () { var r, fetchDone; it("fetchFilter",

    function () { runs(function () { model.fetch(opts) .done(function(data) { r = data; fetchDone = true; }); }); waitsFor(function() {return fetchDone;}, "never completed", 5000); runs(function () { expect(r[0].gender).toBeDefined(); }); }); });
  12. JAsMiNe SPiEs Are: v v v v v v v

    v v v v v v v v v v v v v v v v v ؔ਺ʹ੒Γ୅Θͬͯɺ ͦͷؔ਺͕ԿΛ͍ͯ͠Δͷ͔Λ஌Βͤͯ͘ΕΔɻ
  13. JAsMiNe SPiEs Are: v v v v v v v

    v v v v v v v v v v v v v v v v v  ݺͼग़͞Ε͔ͨͲ͏͔  Կճݺͼग़͞Εͨͷ͔  ͲΜͳҾ਺Ͱݺͼग़͞Ε͔ͨ
  14. describe("an async spec", function () { var r, fetchDone; it("fetchFilter",

    function () { runs(function () { model.fetch(opts) .done(function(data) { r = data; fetchDone = true; }); }); waitsFor(function() {return fetchDone;}, "never completed", 5000); runs(function () { expect(r[0].gender).toBeDefined(); }); }); });
  15. describe("an async spec", function() { var async = new AsyncSpec(this);

    var r; async.beforeEach(function(done){ // simulate async stuff and wait 10ms setTimeout(function(){ model.fetch(opts).done(function(data) { r = data; }); done(); }, 10); }); async.it("fetchFilter", function(done) { expect(res[0].gender).toBeDefined(); }); });
  16. g If Edison had a needle to find in a

    haystack, he would proceed at once with the diligence of the bee to examine straw after straw until he found the object of his search. I was a sorry witness of such doings, knowing that a little theory and calculation would have saved him ninety per cent of his labor.