Jasmine

5,714 views

Published on

0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,714
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
91
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Jasmine

    1. 1. Javascript Specs with Jasmine A Primer for Rubyists
    2. 2. Why Test Javascript?
    3. 3. Seems like alot of work just for some lousyregression tests...
    4. 4. Enforces Good Design•Object Oriented-er• Modular and Namespaced• Flexible, Updatable, Reusable
    5. 5. Focus and Process• Driven by Business Requirements• Red, Green, Refactor• Know When You’re Done
    6. 6. “Free” Documentation•Legible Output• Expresses Object Behavior• Get Devs Up to Speed
    7. 7. Oh yeah... plus you get aregression test suite
    8. 8. Why Use Jasmine?
    9. 9. Friends with RSpec•Very Similar Syntax• “BDD” Approach• Continuous Integration• Borrows Directory Structure• Generative Fixture Support
    10. 10. Pals with Ruby / Rails$ gem install jasmine$ script/generate jasmine$ rake jasmine
    11. 11. Jasmine / RSpec Comparisons Let’s get cookin’...
    12. 12. Spec Suitesdescribe Cook do describe “baking a pie” do # ... endend RSpecdescribe(“Cook”, function() { describe(“baking pie”, function() { // ... });}); Jasmine
    13. 13. Before / Afterbefore(:each) do @pie = Pie.newendafter(:each) { @pie.cleanup } RSpec Jasminevar pie;beforeEach(function(){ pie = new Pie();});afterEach(function() {pie.cleanup()});
    14. 14. Before All / After Allbefore(:all) { setup_code }after(:all) { cleanup_code }describe “something” do # ... tests hereend RSpecsetupCode();describe(“something”, function() { // ... tests here});cleanupCode(); Jasmine
    15. 15. Examples / Expectationsit “should be tasty” do pie = Pie.new pie.should be_tastyend RSpecit(“should be tasty”, function() { var pie = new Pie(); expect(pie).toBeTasty();}); Jasmine
    16. 16. Negative Expectationsit “should not be burned” do pie = Pie.new pie.bake! pie.should_not be_burnedend RSpecit(“should not be burned”, function(){ var pie = new Pie(); pie.bake(); expect(pie).not.toBeBurned();}); Jasmine
    17. 17. Custom MatchersSpec::Matchers.define :be_burned do match do |actual| actual.color == “black” endend RSpecbeforeEach(function() { this.addMatchers({ toBeBurned: function() { return this.actual.color == ‘black’; } });}); Jasmine
    18. 18. Stubbing Valuesit “should cook until 160 degrees” do pie = Pie.new pie.stub!(:temperature).and_return(160) pie.done_baking?.should be_trueend RSpecit(“should cook until 160 degrees”, function() { var pie = new Pie(); spyOn(pie, ‘temperature’).andReturn(160); expect(pie.doneBaking()).toBeTruthy();}); Jasmine
    19. 19. Message Expectationsit “should bake a pie for dinner” do cook = Cook.new cook.should_receive(:bake_a_pie!) cook.make_dinner!end RSpecit(“should bake a pie”, function() { var cook = new Cook(); spyOn(cook, ‘bakePie’); cook.makeDinner(); expect(cook.bakePie).toHaveBeenCalled();}); Jasmine
    20. 20. Expectations w/ Argsit “should bake a TASTY pie” do cook = Cook.new cook.should_receive(:bake_a_pie!).with(‘TASTY’) cook.make_dinner!end RSpecit(“should bake a TASTY pie”, function() { var cook = new Cook(); spyOn(cook, ‘bakePie’); cook.makeDinner(); expect(cook.bakePie) .toHaveBeenCalledWith(‘TASTY’);}); Jasmine
    21. 21. Stub Call-Through RSpecit(“should bake a TASTY pie”, function() { var cook = new Cook(); spyOn(cook, ‘bakePie’).andCallThrough(); cook.makeDinner(); expect(cook.bakePie) .toHaveBeenCalledWith(‘TASTY’);}); Jasmine
    22. 22. Number of Timesit “should check the pie twice” do cook = Cook.new cook.should_receive(:check_the_pie).twice cook.bake_a_pie!end RSpecit(“should check the pie twice”, function() { var cook = new Cook(); spyOn(cook, ‘checkThePie’); cook.bakePie(); expect(cook.checkThePie.callCount).toEqual(2);}); Jasmine
    23. 23. Error Expectationsit “should refuse to bake a pie” do cook = Cook.new(“cranky”) lambda do cook.bake_a_pie! end.should raise_error(MakeYourOwnDamnPie)end RSpecit(“should refuse to bake a pie”, function() { var cook = new Cook(‘cranky’); var msg = “Make Your Own Damn Pie!”; expect(cook.bakePie).toThrow(msg);}); Jasmine
    24. 24. Simulating Errorsit “should catch errs & retry with sudo” do cook = Cook.new cook.stub!(:bake_a_pie!). and_raise(MakeYourOwnDamnPie) cook.should_receive(:sudo_bake_a_pie!) cook.make_dinner!end RSpec
    25. 25. Simulating Errorsit(“should retry with sudo”, function() { var cook = new Cook(); spyOn(cook, ‘bakePie’) .andThrow(“Make Your Own Damn Pie!”); spyOn(cook, ‘sudoBakePie’); expect(cook.sudoBakePie).toHaveBeenCalled();}); Jasmine
    26. 26. Well now that I’mHUNGRYlet’s write some code...

    ×