SlideShare a Scribd company logo
1 of 16
Test Driven Development and Rspec 2 Breccan McLeod-Lundy @breccan
Test Driven Development RED GREEN REFACTOR
A Simple Spec describe ProjectsController do  context "logged in user" do  before(:each) do  @user = User.make  ApplicationController.stub(:current_user).and_return(@user)  End it "should do something" do  @user.should_receive(:do_stuff).and_return(true)  post :do_stuff  end  end  end
Mocks and Stubs Double('classname) mock_model(Model, :variables) thing.stub(:action) { 'value' } Thing.stub_chain(:meth1, :meth2).and_return(cake) double('topic', :title => 'front')
Methods: .should() .should_not() # NOT != .should_receive() .with() .and_return() Actions post :action, :param => “value”
Expectations be_something? #Checks for method something? and calls it. be_valid #Checks for validity
Expectations(cont) .ordered .should_receive(1, 2, 3) .should_receive(“cake”).once .exactly .at_least etc
Expectations(cont) have_key = has_key? have(10).somethings match(/cake/) Thing.stub_chain(:meth1, :meth2).and_return(cake) double('topic', :title => 'front') raise_error(CakeIsLieError) throw_symbol(:cake) be_close(:expected, margin)
Rspec.configure Rspec.configure do |c| c.filter = { :important => true } c.exclusion_filter = { :slow => true } End #Runs this block describe “tests”, :important => true do it “does something” do end #excludes this test it “does something slow”, :slow => true do  end End #You can put lambdas in there too.
Custom Matchers RSpec::Matchers.define :be_in_zone do |zone| match do |player| player.in_zone?(zone) end failure_message_for_should do |player| # generate and return the appropriate string. end failure_message_for_should_not do |player| # generate and return the appropriate string. end   description do   # generate and return the appropriate string. end end
Macros config.extend(ControllerMacros, :type => :controller) # in your Spec::Runner config Say you want: get :index do should_render "index" should_do_stuff end
Create a new ControllerMacros module with methods that construct appropriate it() statements. And add something like: def get(action) define_method :do_request do get action end yield end
Capybara within("li#employee") do fill_in 'Name', :with => 'Jimmy' End save_and_open_page #opens a snapshot of page Capybara.default_wait_time #lets you set how long to wait between actions for ajax when you're checking content.
Capybara + WebDriver $ sudo gem install cucumber cucumber-rails $ ./script/generate cucumber --capybara --rspec $ RAILS_ENV=cucumber rake gems:install Capybara.default_driver = :selenium Or use tags(@javascript etc) and only use selenium for relevant tests: Capybara.javascript_driver = :selenium
Other Important Things Rspec is now in pieces Cucumber Autotest CI
Test Driven Development and Rspec 2 Breccan McLeod-Lundy @breccan P.S. Don't forget Railscamp http://railscamps.com

More Related Content

What's hot

Frontend Finesse with Angular & Rails 4
Frontend Finesse with Angular & Rails 4Frontend Finesse with Angular & Rails 4
Frontend Finesse with Angular & Rails 4undecisive
 
Modern Architecture
Modern ArchitectureModern Architecture
Modern Architectureryandotsmith
 
Simple setup for an Angular EventEmitter
Simple setup for an Angular EventEmitterSimple setup for an Angular EventEmitter
Simple setup for an Angular EventEmittermike2071
 
Boosting Your Productivity, with Backbone & RactiveJS
Boosting Your Productivity, with Backbone & RactiveJS Boosting Your Productivity, with Backbone & RactiveJS
Boosting Your Productivity, with Backbone & RactiveJS Gabriel Gottgtroy Zigolis
 
AngularDart Introduction
AngularDart IntroductionAngularDart Introduction
AngularDart IntroductionNikolaus Graf
 
Introduction tomongodb
Introduction tomongodbIntroduction tomongodb
Introduction tomongodbLee Theobald
 
Mini Curso Django Ii Congresso Academico Ces
Mini Curso Django Ii Congresso Academico CesMini Curso Django Ii Congresso Academico Ces
Mini Curso Django Ii Congresso Academico CesLeonardo Fernandes
 
Grails Simple Login
Grails Simple LoginGrails Simple Login
Grails Simple Loginmoniguna
 
Lessons learned from a huge Rails app - RubyConf Brasil 2019
Lessons learned from a huge Rails app - RubyConf Brasil 2019Lessons learned from a huge Rails app - RubyConf Brasil 2019
Lessons learned from a huge Rails app - RubyConf Brasil 2019Nahuel Garbezza
 

What's hot (14)

Frontend Finesse with Angular & Rails 4
Frontend Finesse with Angular & Rails 4Frontend Finesse with Angular & Rails 4
Frontend Finesse with Angular & Rails 4
 
Compensation
CompensationCompensation
Compensation
 
Modern Architecture
Modern ArchitectureModern Architecture
Modern Architecture
 
Performance
PerformancePerformance
Performance
 
Simple setup for an Angular EventEmitter
Simple setup for an Angular EventEmitterSimple setup for an Angular EventEmitter
Simple setup for an Angular EventEmitter
 
Boosting Your Productivity, with Backbone & RactiveJS
Boosting Your Productivity, with Backbone & RactiveJS Boosting Your Productivity, with Backbone & RactiveJS
Boosting Your Productivity, with Backbone & RactiveJS
 
AngularDart Introduction
AngularDart IntroductionAngularDart Introduction
AngularDart Introduction
 
Ractive js
Ractive jsRactive js
Ractive js
 
Kitchen Design Guide - Consumer Reports
Kitchen Design Guide - Consumer ReportsKitchen Design Guide - Consumer Reports
Kitchen Design Guide - Consumer Reports
 
Introduction tomongodb
Introduction tomongodbIntroduction tomongodb
Introduction tomongodb
 
Mini Curso Django Ii Congresso Academico Ces
Mini Curso Django Ii Congresso Academico CesMini Curso Django Ii Congresso Academico Ces
Mini Curso Django Ii Congresso Academico Ces
 
Grails Simple Login
Grails Simple LoginGrails Simple Login
Grails Simple Login
 
Desktop Summit 2011 Talk
Desktop Summit 2011 TalkDesktop Summit 2011 Talk
Desktop Summit 2011 Talk
 
Lessons learned from a huge Rails app - RubyConf Brasil 2019
Lessons learned from a huge Rails app - RubyConf Brasil 2019Lessons learned from a huge Rails app - RubyConf Brasil 2019
Lessons learned from a huge Rails app - RubyConf Brasil 2019
 

Viewers also liked

Lessons learned while building Omroep.nl
Lessons learned while building Omroep.nlLessons learned while building Omroep.nl
Lessons learned while building Omroep.nlbartzon
 
What's up in ruby for Java developers
What's up in ruby for Java developersWhat's up in ruby for Java developers
What's up in ruby for Java developersThomas Queste
 
Euruko 2009 - DataObjects
Euruko 2009 - DataObjectsEuruko 2009 - DataObjects
Euruko 2009 - DataObjectsDirkjan Bussink
 
Rails Metal, Rack, and Sinatra
Rails Metal, Rack, and SinatraRails Metal, Rack, and Sinatra
Rails Metal, Rack, and SinatraAdam Wiggins
 
Quality Code With Cucumber Presentation
Quality Code With Cucumber PresentationQuality Code With Cucumber Presentation
Quality Code With Cucumber Presentationrailsconf
 
Automated testing with RSpec
Automated testing with RSpecAutomated testing with RSpec
Automated testing with RSpecNascenia IT
 
Straight Up RSpec
Straight Up RSpecStraight Up RSpec
Straight Up RSpecgsterndale
 

Viewers also liked (9)

Lessons learned while building Omroep.nl
Lessons learned while building Omroep.nlLessons learned while building Omroep.nl
Lessons learned while building Omroep.nl
 
What's up in ruby for Java developers
What's up in ruby for Java developersWhat's up in ruby for Java developers
What's up in ruby for Java developers
 
Euruko 2009 - DataObjects
Euruko 2009 - DataObjectsEuruko 2009 - DataObjects
Euruko 2009 - DataObjects
 
RSpec 2 Best practices
RSpec 2 Best practicesRSpec 2 Best practices
RSpec 2 Best practices
 
Rails Metal, Rack, and Sinatra
Rails Metal, Rack, and SinatraRails Metal, Rack, and Sinatra
Rails Metal, Rack, and Sinatra
 
Quality Code With Cucumber Presentation
Quality Code With Cucumber PresentationQuality Code With Cucumber Presentation
Quality Code With Cucumber Presentation
 
Automated testing with RSpec
Automated testing with RSpecAutomated testing with RSpec
Automated testing with RSpec
 
Rtt preso
Rtt presoRtt preso
Rtt preso
 
Straight Up RSpec
Straight Up RSpecStraight Up RSpec
Straight Up RSpec
 

Similar to Well

Ruby on rails
Ruby on rails Ruby on rails
Ruby on rails Mohit Jain
 
Ruby on Rails testing with Rspec
Ruby on Rails testing with RspecRuby on Rails testing with Rspec
Ruby on Rails testing with RspecBunlong Van
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialYi-Ting Cheng
 
Testing C# and ASP.net using Ruby
Testing C# and ASP.net using RubyTesting C# and ASP.net using Ruby
Testing C# and ASP.net using RubyBen Hall
 
Task scheduling in laravel 8 tutorial
Task scheduling in laravel 8 tutorialTask scheduling in laravel 8 tutorial
Task scheduling in laravel 8 tutorialKaty Slemon
 
RSpec User Stories
RSpec User StoriesRSpec User Stories
RSpec User Storiesrahoulb
 
What's Coming in Spring 3.0
What's Coming in Spring 3.0What's Coming in Spring 3.0
What's Coming in Spring 3.0Matt Raible
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Codescidept
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran MizrahiIntro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran MizrahiRan Mizrahi
 
ActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group PresentationActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group Presentationipolevoy
 
27 - Panorama Necto 14 component mode & java script - visualization & data di...
27 - Panorama Necto 14 component mode & java script - visualization & data di...27 - Panorama Necto 14 component mode & java script - visualization & data di...
27 - Panorama Necto 14 component mode & java script - visualization & data di...Panorama Software
 
SproutCore is Awesome - HTML5 Summer DevFest
SproutCore is Awesome - HTML5 Summer DevFestSproutCore is Awesome - HTML5 Summer DevFest
SproutCore is Awesome - HTML5 Summer DevFesttomdale
 
Ways to Set Focus on an Input Field After Rendering in React.pptx
Ways to Set Focus on an Input Field After Rendering in React.pptxWays to Set Focus on an Input Field After Rendering in React.pptx
Ways to Set Focus on an Input Field After Rendering in React.pptxBOSC Tech Labs
 
What's new in Rails 2?
What's new in Rails 2?What's new in Rails 2?
What's new in Rails 2?brynary
 
Standardizing JavaScript Decorators in TC39 (Full Stack Fest 2019)
Standardizing JavaScript Decorators in TC39 (Full Stack Fest 2019)Standardizing JavaScript Decorators in TC39 (Full Stack Fest 2019)
Standardizing JavaScript Decorators in TC39 (Full Stack Fest 2019)Igalia
 

Similar to Well (20)

Ruby on rails
Ruby on rails Ruby on rails
Ruby on rails
 
Ruby on Rails testing with Rspec
Ruby on Rails testing with RspecRuby on Rails testing with Rspec
Ruby on Rails testing with Rspec
 
Controller specs
Controller specsController specs
Controller specs
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails Turtorial
 
Testing C# and ASP.net using Ruby
Testing C# and ASP.net using RubyTesting C# and ASP.net using Ruby
Testing C# and ASP.net using Ruby
 
Task scheduling in laravel 8 tutorial
Task scheduling in laravel 8 tutorialTask scheduling in laravel 8 tutorial
Task scheduling in laravel 8 tutorial
 
RSpec User Stories
RSpec User StoriesRSpec User Stories
RSpec User Stories
 
What's Coming in Spring 3.0
What's Coming in Spring 3.0What's Coming in Spring 3.0
What's Coming in Spring 3.0
 
Refactoring
RefactoringRefactoring
Refactoring
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran MizrahiIntro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran Mizrahi
 
ActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group PresentationActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group Presentation
 
27 - Panorama Necto 14 component mode & java script - visualization & data di...
27 - Panorama Necto 14 component mode & java script - visualization & data di...27 - Panorama Necto 14 component mode & java script - visualization & data di...
27 - Panorama Necto 14 component mode & java script - visualization & data di...
 
SproutCore is Awesome - HTML5 Summer DevFest
SproutCore is Awesome - HTML5 Summer DevFestSproutCore is Awesome - HTML5 Summer DevFest
SproutCore is Awesome - HTML5 Summer DevFest
 
Ways to Set Focus on an Input Field After Rendering in React.pptx
Ways to Set Focus on an Input Field After Rendering in React.pptxWays to Set Focus on an Input Field After Rendering in React.pptx
Ways to Set Focus on an Input Field After Rendering in React.pptx
 
What's new in Rails 2?
What's new in Rails 2?What's new in Rails 2?
What's new in Rails 2?
 
TDC 2015 - DSLs em Ruby
TDC 2015 - DSLs em RubyTDC 2015 - DSLs em Ruby
TDC 2015 - DSLs em Ruby
 
Standardizing JavaScript Decorators in TC39 (Full Stack Fest 2019)
Standardizing JavaScript Decorators in TC39 (Full Stack Fest 2019)Standardizing JavaScript Decorators in TC39 (Full Stack Fest 2019)
Standardizing JavaScript Decorators in TC39 (Full Stack Fest 2019)
 
Combres
CombresCombres
Combres
 
Rspec
RspecRspec
Rspec
 

Well

  • 1. Test Driven Development and Rspec 2 Breccan McLeod-Lundy @breccan
  • 2. Test Driven Development RED GREEN REFACTOR
  • 3. A Simple Spec describe ProjectsController do context "logged in user" do before(:each) do @user = User.make ApplicationController.stub(:current_user).and_return(@user) End it "should do something" do @user.should_receive(:do_stuff).and_return(true) post :do_stuff end end end
  • 4. Mocks and Stubs Double('classname) mock_model(Model, :variables) thing.stub(:action) { 'value' } Thing.stub_chain(:meth1, :meth2).and_return(cake) double('topic', :title => 'front')
  • 5. Methods: .should() .should_not() # NOT != .should_receive() .with() .and_return() Actions post :action, :param => “value”
  • 6. Expectations be_something? #Checks for method something? and calls it. be_valid #Checks for validity
  • 7. Expectations(cont) .ordered .should_receive(1, 2, 3) .should_receive(“cake”).once .exactly .at_least etc
  • 8. Expectations(cont) have_key = has_key? have(10).somethings match(/cake/) Thing.stub_chain(:meth1, :meth2).and_return(cake) double('topic', :title => 'front') raise_error(CakeIsLieError) throw_symbol(:cake) be_close(:expected, margin)
  • 9. Rspec.configure Rspec.configure do |c| c.filter = { :important => true } c.exclusion_filter = { :slow => true } End #Runs this block describe “tests”, :important => true do it “does something” do end #excludes this test it “does something slow”, :slow => true do end End #You can put lambdas in there too.
  • 10. Custom Matchers RSpec::Matchers.define :be_in_zone do |zone| match do |player| player.in_zone?(zone) end failure_message_for_should do |player| # generate and return the appropriate string. end failure_message_for_should_not do |player| # generate and return the appropriate string. end description do # generate and return the appropriate string. end end
  • 11. Macros config.extend(ControllerMacros, :type => :controller) # in your Spec::Runner config Say you want: get :index do should_render "index" should_do_stuff end
  • 12. Create a new ControllerMacros module with methods that construct appropriate it() statements. And add something like: def get(action) define_method :do_request do get action end yield end
  • 13. Capybara within("li#employee") do fill_in 'Name', :with => 'Jimmy' End save_and_open_page #opens a snapshot of page Capybara.default_wait_time #lets you set how long to wait between actions for ajax when you're checking content.
  • 14. Capybara + WebDriver $ sudo gem install cucumber cucumber-rails $ ./script/generate cucumber --capybara --rspec $ RAILS_ENV=cucumber rake gems:install Capybara.default_driver = :selenium Or use tags(@javascript etc) and only use selenium for relevant tests: Capybara.javascript_driver = :selenium
  • 15. Other Important Things Rspec is now in pieces Cucumber Autotest CI
  • 16. Test Driven Development and Rspec 2 Breccan McLeod-Lundy @breccan P.S. Don't forget Railscamp http://railscamps.com