Jasmine
Upcoming SlideShare
Loading in...5
×
 

Jasmine

on

  • 4,038 views

 

Statistics

Views

Total Views
4,038
Views on SlideShare
4,038
Embed Views
0

Actions

Likes
6
Downloads
23
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \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 Jasmine Presentation Transcript

  • Javascript Specs with Jasmine A Primer for Rubyists
  • Why Test Javascript?
  • Seems like alot of work just for some lousyregression tests...
  • Enforces Good Design•Object Oriented-er• Modular and Namespaced• Flexible, Updatable, Reusable
  • Focus and Process• Driven by Business Requirements• Red, Green, Refactor• Know When You’re Done
  • “Free” Documentation•Legible Output• Expresses Object Behavior• Get Devs Up to Speed
  • Oh yeah... plus you get aregression test suite
  • Why Use Jasmine?
  • Friends with RSpec•Very Similar Syntax• “BDD” Approach• Continuous Integration• Borrows Directory Structure• Generative Fixture Support
  • Pals with Ruby / Rails$ gem install jasmine$ script/generate jasmine$ rake jasmine
  • Jasmine / RSpec Comparisons Let’s get cookin’...
  • Spec Suitesdescribe Cook do describe “baking a pie” do # ... endend RSpecdescribe(“Cook”, function() { describe(“baking pie”, function() { // ... });}); Jasmine
  • Before / Afterbefore(:each) do @pie = Pie.newendafter(:each) { @pie.cleanup } RSpec Jasminevar pie;beforeEach(function(){ pie = new Pie();});afterEach(function() {pie.cleanup()});
  • Before All / After Allbefore(:all) { setup_code }after(:all) { cleanup_code }describe “something” do # ... tests hereend RSpecsetupCode();describe(“something”, function() { // ... tests here});cleanupCode(); Jasmine
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Well now that I’mHUNGRYlet’s write some code...