SlideShare une entreprise Scribd logo
1  sur  26
Javascript Specs
  with Jasmine
  A Primer for Rubyists
Why Test Javascript?
Seems like a


lot of work
    just for some lousy

regression 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 a
regression 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 Suites
describe Cook do
  describe “baking a pie” do
    # ...
  end
end
                                    RSpec


describe(“Cook”, function() {
  describe(“baking pie”, function() {
    // ...
  });
});
                                  Jasmine
Before / After
before(:each) do
  @pie = Pie.new
end

after(:each) { @pie.cleanup }
                                    RSpec

                                  Jasmine
var pie;
beforeEach(function(){
  pie = new Pie();
});
afterEach(function() {pie.cleanup()});
Before All / After All
before(:all) { setup_code }
after(:all) { cleanup_code }
describe “something” do
  # ... tests here
end
                                      RSpec



setupCode();
describe(“something”, function() {
  // ... tests here
});
cleanupCode();
                                     Jasmine
Examples / Expectations
it “should be tasty” do
  pie = Pie.new
  pie.should be_tasty
end
                                      RSpec


it(“should be tasty”, function() {
  var pie = new Pie();
  expect(pie).toBeTasty();
});
                                     Jasmine
Negative Expectations
it “should not be burned” do
  pie = Pie.new
  pie.bake!
  pie.should_not be_burned
end
                                    RSpec


it(“should not be burned”, function(){
  var pie = new Pie();
  pie.bake();
  expect(pie).not.toBeBurned();
});
                                  Jasmine
Custom Matchers
Spec::Matchers.define :be_burned do
  match do |actual|
    actual.color == “black”
  end
end
                                         RSpec


beforeEach(function() {
  this.addMatchers({
    toBeBurned: function() {
      return this.actual.color == ‘black’;
    }
  });
});
                                       Jasmine
Stubbing Values
it “should cook until 160 degrees” do
  pie = Pie.new
  pie.stub!(:temperature).and_return(160)
  pie.done_baking?.should be_true
end
                                              RSpec



it(“should cook until 160 degrees”, function() {
  var pie = new Pie();
  spyOn(pie, ‘temperature’).andReturn(160);
  expect(pie.doneBaking()).toBeTruthy();
});
                                            Jasmine
Message Expectations
it “should bake a pie for dinner” do
  cook = Cook.new
  cook.should_receive(:bake_a_pie!)
  cook.make_dinner!
end
                                             RSpec


it(“should bake a pie”, function() {
  var cook = new Cook();
  spyOn(cook, ‘bakePie’);
  cook.makeDinner();
  expect(cook.bakePie).toHaveBeenCalled();
});
                                         Jasmine
Expectations w/ Args
it “should bake a TASTY pie” do
  cook = Cook.new
  cook.should_receive(:bake_a_pie!).with(‘TASTY’)
  cook.make_dinner!
end
                                              RSpec


it(“should bake a TASTY pie”, function() {
  var cook = new Cook();
  spyOn(cook, ‘bakePie’);
  cook.makeDinner();
  expect(cook.bakePie)
    .toHaveBeenCalledWith(‘TASTY’);
});
                                             Jasmine
Stub Call-Through

            RSpec
it(“should bake a TASTY pie”, function() {
  var cook = new Cook();
  spyOn(cook, ‘bakePie’).andCallThrough();
  cook.makeDinner();
  expect(cook.bakePie)
    .toHaveBeenCalledWith(‘TASTY’);
});
                                             Jasmine
Number of Times
it “should check the pie twice” do
  cook = Cook.new
  cook.should_receive(:check_the_pie).twice
  cook.bake_a_pie!
end
                                               RSpec



it(“should check the pie twice”, function() {
  var cook = new Cook();
  spyOn(cook, ‘checkThePie’);
  cook.bakePie();
  expect(cook.checkThePie.callCount).toEqual(2);
});
                                              Jasmine
Error Expectations
it “should refuse to bake a pie” do
  cook = Cook.new(“cranky”)
  lambda do
    cook.bake_a_pie!
  end.should raise_error(MakeYourOwnDamnPie)
end
                                                 RSpec



it(“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 Errors
it “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 Errors
it(“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’m

HUNGRY
let’s write some code...

Contenu connexe

En vedette

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Testing Javascript with Jasmine - A Primer for Rubyists

  • 1. Javascript Specs with Jasmine A Primer for Rubyists
  • 3. Seems like a lot of work just for some lousy regression tests...
  • 4. Enforces Good Design •Object Oriented-er • Modular and Namespaced • Flexible, Updatable, Reusable
  • 5. Focus and Process • Driven by Business Requirements • Red, Green, Refactor • Know When You’re Done
  • 6. “Free” Documentation •Legible Output • Expresses Object Behavior • Get Devs Up to Speed
  • 7. Oh yeah... plus you get a regression test suite
  • 9. Friends with RSpec •Very Similar Syntax • “BDD” Approach • Continuous Integration • Borrows Directory Structure • Generative Fixture Support
  • 10. Pals with Ruby / Rails $ gem install jasmine $ script/generate jasmine $ rake jasmine
  • 11. Jasmine / RSpec Comparisons Let’s get cookin’...
  • 12. Spec Suites describe Cook do describe “baking a pie” do # ... end end RSpec describe(“Cook”, function() { describe(“baking pie”, function() { // ... }); }); Jasmine
  • 13. Before / After before(:each) do @pie = Pie.new end after(:each) { @pie.cleanup } RSpec Jasmine var pie; beforeEach(function(){ pie = new Pie(); }); afterEach(function() {pie.cleanup()});
  • 14. Before All / After All before(:all) { setup_code } after(:all) { cleanup_code } describe “something” do # ... tests here end RSpec setupCode(); describe(“something”, function() { // ... tests here }); cleanupCode(); Jasmine
  • 15. Examples / Expectations it “should be tasty” do pie = Pie.new pie.should be_tasty end RSpec it(“should be tasty”, function() { var pie = new Pie(); expect(pie).toBeTasty(); }); Jasmine
  • 16. Negative Expectations it “should not be burned” do pie = Pie.new pie.bake! pie.should_not be_burned end RSpec it(“should not be burned”, function(){ var pie = new Pie(); pie.bake(); expect(pie).not.toBeBurned(); }); Jasmine
  • 17. Custom Matchers Spec::Matchers.define :be_burned do match do |actual| actual.color == “black” end end RSpec beforeEach(function() { this.addMatchers({ toBeBurned: function() { return this.actual.color == ‘black’; } }); }); Jasmine
  • 18. Stubbing Values it “should cook until 160 degrees” do pie = Pie.new pie.stub!(:temperature).and_return(160) pie.done_baking?.should be_true end RSpec it(“should cook until 160 degrees”, function() { var pie = new Pie(); spyOn(pie, ‘temperature’).andReturn(160); expect(pie.doneBaking()).toBeTruthy(); }); Jasmine
  • 19. Message Expectations it “should bake a pie for dinner” do cook = Cook.new cook.should_receive(:bake_a_pie!) cook.make_dinner! end RSpec it(“should bake a pie”, function() { var cook = new Cook(); spyOn(cook, ‘bakePie’); cook.makeDinner(); expect(cook.bakePie).toHaveBeenCalled(); }); Jasmine
  • 20. Expectations w/ Args it “should bake a TASTY pie” do cook = Cook.new cook.should_receive(:bake_a_pie!).with(‘TASTY’) cook.make_dinner! end RSpec it(“should bake a TASTY pie”, function() { var cook = new Cook(); spyOn(cook, ‘bakePie’); cook.makeDinner(); expect(cook.bakePie) .toHaveBeenCalledWith(‘TASTY’); }); Jasmine
  • 21. Stub Call-Through RSpec it(“should bake a TASTY pie”, function() { var cook = new Cook(); spyOn(cook, ‘bakePie’).andCallThrough(); cook.makeDinner(); expect(cook.bakePie) .toHaveBeenCalledWith(‘TASTY’); }); Jasmine
  • 22. Number of Times it “should check the pie twice” do cook = Cook.new cook.should_receive(:check_the_pie).twice cook.bake_a_pie! end RSpec it(“should check the pie twice”, function() { var cook = new Cook(); spyOn(cook, ‘checkThePie’); cook.bakePie(); expect(cook.checkThePie.callCount).toEqual(2); }); Jasmine
  • 23. Error Expectations it “should refuse to bake a pie” do cook = Cook.new(“cranky”) lambda do cook.bake_a_pie! end.should raise_error(MakeYourOwnDamnPie) end RSpec it(“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. Simulating Errors it “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. Simulating Errors it(“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. Well now that I’m HUNGRY let’s write some code...

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n