SlideShare a Scribd company logo
Efficient Rails Test-Driven
       Development

     Wolfram Arnold
    www.rubyfocus.biz

    In collaboration with:
Sarah Allen, BlazingCloud.net
       marakana.com
Class Outline

Week #1
  The economics of testing
  Testing in layers, design patterns
  Toolbox: RSpec with Rails
  RSpec & Models


Week #2
  A culture of testing: Why TDD? How to TDD?
  Testing & Data Dependencies
  Toolbox: Fixtures, Factories, Mocks & Stubs
Class Outline

Week #3
  Controller testing
  View, Helper, Routes Testing
  How much is enough? How much is too much?


Week #4
  Refactoring code & tests, custom matchers
  API Testing
  Remote data setup
  Cucumber for API testing & documentation
Class Outline

Week #5
  Integration Testing—when and how?
  Toolbox: Cucumber, Selenium & Friends
  Page Object Pattern


Week #6
  TDD—what next?
  BDD, Agile Process, XP → VDD: Value-Driven
   Development
  Advanced/Special Requests
What you can expect

Presentation, Examples
Answers to questions
A range of material from current development
  practice
Homework
Fluidity & adaptability
Fun
It works best, when...

Active participation
Try something new
Focus
Team Effort
  Pairing
  Utilizing the resources in class: TA's, participants
Discussions in public
  Class Q&A
  Mailing list
Efficient Rails Test-Driven
       Development
Why “efficient” and “testing”?

“Testing takes too much time.”
“It's more efficient to test later.”
“Testing is the responsibility of QA, not
  developers.”
“It's not practical to test X.”
   Tests break when data changes.
   Tests break when design changes.
The Role of Testing

Development without tests...
  fails to empower developers to efficiently take
    responsibility for quality of the code delivered
  makes collaboration harder
  build narrow silos of expertise
  instills fear & resistance to change
  makes documentation a chore
  stops being efficient very soon
TDD: Keeping cost of change low
Cost per change
                              without
                               TDD




                                  with
                                  TDD




                                   Time
Why?

Non-TDD
 Accumulates “technical debt” unchecked
 Removal of technical debt carries risk
    The more technical debt, the higher the risk
 Existing technical debt attracts more technical debt
    Like compound interest
    People are most likely to do what others did before them
    To break the pattern heroic discipline & coordination
      required
Testing in Layers

 Application, Browser UI           Selenium


                              RSpec Integration       Test::Unit Integration
   Application, Server
                              Cucumber, Webrat

                            RSpec           RSpec
 Views           Helpers
                            Views           Helpers

                            RSpec           RSpec     Test::Unit Functional
Controller       Routes
                           Controller       Routes

                                    RSpec             Test::Unit
         Model
                                    Model
Cost of Testing
                           Layers


 Application, Browser UI



   Application, Server



 Views           Helpers



Controller       Routes



         Model



                                    Cost
Cost of Testing
                           Relationship to data


                           most
 Application, Browser UI   removed


   Application, Server



 Views           Helpers



Controller       Routes



         Model             closest



                                                  Cost
Best ROI for Testing
                           Layers


 Application, Browser UI



   Application, Server



 Views           Helpers



Controller       Routes



         Model



                                    Impact/Line of Test Code
TDD & Design Patterns

Skinny Controller—   ➢   Designed to move logic
 Fat Model               from higher to lower
DRY                      application layers
named_scope          ➢   Following design
                         patterns makes testing
Proxy Associations       easier
Validations          ➢   Code written following
...                      TDD economics will
                         naturally converge on
                         these design patterns!
Structure of Tests

Setup
Expected value
Actual value
Verification: actual == expected?
Teardown
Good Tests are...

Compact
Responsible for testing one concern only
Fast
DRY
RSpec Verifications

should respond_to
should be_nil
   → works with any ? method (so-called “predicates”)
should be_valid
should_not be_nil; should_not be_valid
lambda {...}.should change(), {}, .from().to(), .by()
should eql, ==, equal
RSpec Structure

before, before(:each), before(:all)
after, after(:each), after(:all)
describe do...end, nested
it do... end
Let's do some coding

Demo
Models: What to test?

Validation Rules
Associations
Any custom method
Association Proxy Methods
Let's do some coding

Exercise, but wait...
Test-First Teaching

Pioneered by Sarah Allen
  Instructor writes tests
  Students write code


What about writing tests?
Behavior-First Teaching

Instructor specifies desired behavior
  in plain English
  like a user story
Students write tests
Students write code to make tests pass
Story Exercise #1

A Person object must have a first and last name.
A Person object can construct a full name from
  the first and last name.
A Person object has an optional middle name.
A Person object returns a full name including, if
  present, the middle name.
RSpec ==, eql, equal

obj.should == 5            5 == 5
obj.should eql(5)          5.eql 5
obj.should equal(5)        5.equal 5
Object Equality vs. Identity         Use ==
eql, ==      compare values          Unless you know you
                                     need something else
equal, === compare objects,
           classes

          Warning! Do not use != with RSpec.
          Use should_not instead.
RSpec should change

lambda {
  Person.create
}.should change(Person, :count).by(1)


lambda {
  @bob.addresses.create(:street => “...”)
}.should change(@bob.addresses, :count).by(1)

              nd
Warning: The 2 example is incorrect!
RSpec should change

lambda {
  # code that causes a change
}.should change(object, :method).by(difference)
or
should change(object,
  :method).from(initial).to(final)


The object typically is a class, if not, be wary of
 reload!
should change with block

lambda {
  # code that causes a change
}.should change { expression }.by(difference)
or
should change { expression }.from(initial).to(final)
better with block

lambda {
  @bob.addresses.create(:street => “...”)
}.should change {
  @bob.addresses(true).count }.by(1)
Homework

A Person should save correctly.
A Person can have many Addresses.
A Person's Address must have a street, city and
  zip.
A Person's Address can have an optional country.
If the country is left blank, it should default to
   “USA” prior to saving.
BizConf

                                Aug 4-6, 2010
                                Amelia Island, FL
                                Discount code: WOLF
Rails & Web App
Professionals, Entrepreneurs,
                                for 43% off
Consultants                     http://bizconf.org?
Small group                       coupon=WOLF
Network with Who's Who
Organized by Obie
Fernandez of Hashrocket
Highlight: David Allen

More Related Content

What's hot

Angular Application Testing
Angular Application TestingAngular Application Testing
Angular Application Testing
Troy Miles
 
Karate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made SimpleKarate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made Simple
VodqaBLR
 
API Test Automation Using Karate (Anil Kumar Moka)
API Test Automation Using Karate (Anil Kumar Moka)API Test Automation Using Karate (Anil Kumar Moka)
API Test Automation Using Karate (Anil Kumar Moka)
Peter Thomas
 
Karate - MoT Dallas 26-Oct-2017
Karate - MoT Dallas 26-Oct-2017Karate - MoT Dallas 26-Oct-2017
Karate - MoT Dallas 26-Oct-2017
Peter Thomas
 
Real World Fun with ActiveResource
Real World Fun with ActiveResourceReal World Fun with ActiveResource
Real World Fun with ActiveResource
Rob C
 
Automate right start from API
Automate right start from APIAutomate right start from API
Automate right start from API
Roman Liubun
 
Refactoring @ Mindvalley: Smells, Techniques and Patterns
Refactoring @ Mindvalley: Smells, Techniques and PatternsRefactoring @ Mindvalley: Smells, Techniques and Patterns
Refactoring @ Mindvalley: Smells, Techniques and Patterns
Tristan Gomez
 
Behavior Driven Development with Cucumber
Behavior Driven Development with CucumberBehavior Driven Development with Cucumber
Behavior Driven Development with Cucumber
Asheesh Mehdiratta
 
django Forms in a Web API World
django Forms in a Web API Worlddjango Forms in a Web API World
django Forms in a Web API World
Tareque Hossain
 
BDD style Unit Testing
BDD style Unit TestingBDD style Unit Testing
BDD style Unit TestingWen-Tien Chang
 
Using the latest Java Persistence API 2 Features - Tech Days 2010 India
Using the latest Java Persistence API 2 Features - Tech Days 2010 IndiaUsing the latest Java Persistence API 2 Features - Tech Days 2010 India
Using the latest Java Persistence API 2 Features - Tech Days 2010 India
Arun Gupta
 
A Deep Dive into the W3C WebDriver Specification
A Deep Dive into the W3C WebDriver SpecificationA Deep Dive into the W3C WebDriver Specification
A Deep Dive into the W3C WebDriver Specification
Peter Thomas
 
Karate DSL
Karate DSLKarate DSL
Karate DSL
anil borse
 
2016 - Serverless Microservices on AWS with API Gateway and Lambda
2016 - Serverless Microservices on AWS with API Gateway and Lambda2016 - Serverless Microservices on AWS with API Gateway and Lambda
2016 - Serverless Microservices on AWS with API Gateway and Lambda
devopsdaysaustin
 
Metaprogramming JavaScript
Metaprogramming  JavaScriptMetaprogramming  JavaScript
Metaprogramming JavaScript
danwrong
 
Refactoring
RefactoringRefactoring
Refactoring
Artem Tabalin
 
Introduction to testing in Rails
Introduction to testing in RailsIntroduction to testing in Rails
Introduction to testing in Rails
benlcollins
 
ExpertTalks Manchester September 2018
ExpertTalks Manchester September 2018ExpertTalks Manchester September 2018
ExpertTalks Manchester September 2018
Tessa Ledwith
 
Ibm_interconnect_restapi_workshop
Ibm_interconnect_restapi_workshopIbm_interconnect_restapi_workshop
Ibm_interconnect_restapi_workshop
Shubhra Kar
 
Cqrs api
Cqrs apiCqrs api
Cqrs api
Brandon Mueller
 

What's hot (20)

Angular Application Testing
Angular Application TestingAngular Application Testing
Angular Application Testing
 
Karate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made SimpleKarate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made Simple
 
API Test Automation Using Karate (Anil Kumar Moka)
API Test Automation Using Karate (Anil Kumar Moka)API Test Automation Using Karate (Anil Kumar Moka)
API Test Automation Using Karate (Anil Kumar Moka)
 
Karate - MoT Dallas 26-Oct-2017
Karate - MoT Dallas 26-Oct-2017Karate - MoT Dallas 26-Oct-2017
Karate - MoT Dallas 26-Oct-2017
 
Real World Fun with ActiveResource
Real World Fun with ActiveResourceReal World Fun with ActiveResource
Real World Fun with ActiveResource
 
Automate right start from API
Automate right start from APIAutomate right start from API
Automate right start from API
 
Refactoring @ Mindvalley: Smells, Techniques and Patterns
Refactoring @ Mindvalley: Smells, Techniques and PatternsRefactoring @ Mindvalley: Smells, Techniques and Patterns
Refactoring @ Mindvalley: Smells, Techniques and Patterns
 
Behavior Driven Development with Cucumber
Behavior Driven Development with CucumberBehavior Driven Development with Cucumber
Behavior Driven Development with Cucumber
 
django Forms in a Web API World
django Forms in a Web API Worlddjango Forms in a Web API World
django Forms in a Web API World
 
BDD style Unit Testing
BDD style Unit TestingBDD style Unit Testing
BDD style Unit Testing
 
Using the latest Java Persistence API 2 Features - Tech Days 2010 India
Using the latest Java Persistence API 2 Features - Tech Days 2010 IndiaUsing the latest Java Persistence API 2 Features - Tech Days 2010 India
Using the latest Java Persistence API 2 Features - Tech Days 2010 India
 
A Deep Dive into the W3C WebDriver Specification
A Deep Dive into the W3C WebDriver SpecificationA Deep Dive into the W3C WebDriver Specification
A Deep Dive into the W3C WebDriver Specification
 
Karate DSL
Karate DSLKarate DSL
Karate DSL
 
2016 - Serverless Microservices on AWS with API Gateway and Lambda
2016 - Serverless Microservices on AWS with API Gateway and Lambda2016 - Serverless Microservices on AWS with API Gateway and Lambda
2016 - Serverless Microservices on AWS with API Gateway and Lambda
 
Metaprogramming JavaScript
Metaprogramming  JavaScriptMetaprogramming  JavaScript
Metaprogramming JavaScript
 
Refactoring
RefactoringRefactoring
Refactoring
 
Introduction to testing in Rails
Introduction to testing in RailsIntroduction to testing in Rails
Introduction to testing in Rails
 
ExpertTalks Manchester September 2018
ExpertTalks Manchester September 2018ExpertTalks Manchester September 2018
ExpertTalks Manchester September 2018
 
Ibm_interconnect_restapi_workshop
Ibm_interconnect_restapi_workshopIbm_interconnect_restapi_workshop
Ibm_interconnect_restapi_workshop
 
Cqrs api
Cqrs apiCqrs api
Cqrs api
 

Viewers also liked

Acts As Most Popular
Acts As Most PopularActs As Most Popular
Acts As Most Popular
Wolfram Arnold
 
Nine at Mary Brickell Village brochure
Nine at Mary Brickell Village brochureNine at Mary Brickell Village brochure
Nine at Mary Brickell Village brochure
Lucas Lechuga
 
Cache Money Talk: Practical Application
Cache Money Talk: Practical ApplicationCache Money Talk: Practical Application
Cache Money Talk: Practical Application
Wolfram Arnold
 
Paraiso Bay floor plans
Paraiso Bay floor plansParaiso Bay floor plans
Paraiso Bay floor plans
Lucas Lechuga
 
2009 07 21: Nested Attributes
2009 07 21: Nested Attributes2009 07 21: Nested Attributes
2009 07 21: Nested Attributes
Wolfram Arnold
 
Axis Brickell Floor Plans
Axis Brickell Floor PlansAxis Brickell Floor Plans
Axis Brickell Floor Plans
Lucas Lechuga
 
Faena House floor plans
Faena House floor plansFaena House floor plans
Faena House floor plans
Lucas Lechuga
 

Viewers also liked (7)

Acts As Most Popular
Acts As Most PopularActs As Most Popular
Acts As Most Popular
 
Nine at Mary Brickell Village brochure
Nine at Mary Brickell Village brochureNine at Mary Brickell Village brochure
Nine at Mary Brickell Village brochure
 
Cache Money Talk: Practical Application
Cache Money Talk: Practical ApplicationCache Money Talk: Practical Application
Cache Money Talk: Practical Application
 
Paraiso Bay floor plans
Paraiso Bay floor plansParaiso Bay floor plans
Paraiso Bay floor plans
 
2009 07 21: Nested Attributes
2009 07 21: Nested Attributes2009 07 21: Nested Attributes
2009 07 21: Nested Attributes
 
Axis Brickell Floor Plans
Axis Brickell Floor PlansAxis Brickell Floor Plans
Axis Brickell Floor Plans
 
Faena House floor plans
Faena House floor plansFaena House floor plans
Faena House floor plans
 

Similar to 2010-07-19_rails_tdd_week1

Pragmatic Architecture in .NET
Pragmatic Architecture in .NETPragmatic Architecture in .NET
Pragmatic Architecture in .NET
housecor
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
Gautam Rege
 
Agile Development in .NET
Agile Development in .NETAgile Development in .NET
Agile Development in .NET
danhermes
 
The "Holy Grail" of Dev/Ops
The "Holy Grail" of Dev/OpsThe "Holy Grail" of Dev/Ops
The "Holy Grail" of Dev/Ops
Erik Osterman
 
Sr_Lead_QA_April_2016
Sr_Lead_QA_April_2016Sr_Lead_QA_April_2016
Sr_Lead_QA_April_2016Nick Yefimov
 
Better and Faster: A Journey Toward Clean Code and Enjoyment
Better and Faster: A Journey Toward Clean Code and EnjoymentBetter and Faster: A Journey Toward Clean Code and Enjoyment
Better and Faster: A Journey Toward Clean Code and Enjoyment
Chris Holland
 
CucumberSeleniumWD
CucumberSeleniumWDCucumberSeleniumWD
CucumberSeleniumWDVikas Sarin
 
AWS and Dynatrace: Moving your Cloud Strategy to the Next Level
AWS and Dynatrace: Moving your Cloud Strategy to the Next LevelAWS and Dynatrace: Moving your Cloud Strategy to the Next Level
AWS and Dynatrace: Moving your Cloud Strategy to the Next Level
Dynatrace
 
Measuring Your Code
Measuring Your CodeMeasuring Your Code
Measuring Your Code
Nate Abele
 
Measuring Your Code 2.0
Measuring Your Code 2.0Measuring Your Code 2.0
Measuring Your Code 2.0
Nate Abele
 
Behavior Driven Development by Example
Behavior Driven Development by ExampleBehavior Driven Development by Example
Behavior Driven Development by Example
Nalin Goonawardana
 
The Next Five Years of Rails
The Next Five Years of RailsThe Next Five Years of Rails
The Next Five Years of Rails
Alex Mercer
 
Cost Effective Web Application Testing
Cost Effective Web Application TestingCost Effective Web Application Testing
Cost Effective Web Application Testing
Hari Pudipeddi
 
Cost effective web application testing
Cost effective web application testingCost effective web application testing
Cost effective web application testing
Harinath Pudipeddi
 
Cost effective web application testing
Cost effective web application testingCost effective web application testing
Cost effective web application testingHarinath Pudipeddi
 
Resume debasish
Resume debasish Resume debasish
Resume debasish
Debasish Hotta
 
Dot NET Solution Architect Roadmap By Scholarhat PDF
Dot NET Solution Architect Roadmap By Scholarhat PDFDot NET Solution Architect Roadmap By Scholarhat PDF
Dot NET Solution Architect Roadmap By Scholarhat PDF
Scholarhat
 
Onion Architecture with S#arp
Onion Architecture with S#arpOnion Architecture with S#arp
Onion Architecture with S#arp
Gary Pedretti
 
Approaching ATDD/BDD
Approaching ATDD/BDDApproaching ATDD/BDD
Approaching ATDD/BDD
Dhaval Dalal
 
Coding Naked
Coding NakedCoding Naked
Coding Naked
Caleb Jenkins
 

Similar to 2010-07-19_rails_tdd_week1 (20)

Pragmatic Architecture in .NET
Pragmatic Architecture in .NETPragmatic Architecture in .NET
Pragmatic Architecture in .NET
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Agile Development in .NET
Agile Development in .NETAgile Development in .NET
Agile Development in .NET
 
The "Holy Grail" of Dev/Ops
The "Holy Grail" of Dev/OpsThe "Holy Grail" of Dev/Ops
The "Holy Grail" of Dev/Ops
 
Sr_Lead_QA_April_2016
Sr_Lead_QA_April_2016Sr_Lead_QA_April_2016
Sr_Lead_QA_April_2016
 
Better and Faster: A Journey Toward Clean Code and Enjoyment
Better and Faster: A Journey Toward Clean Code and EnjoymentBetter and Faster: A Journey Toward Clean Code and Enjoyment
Better and Faster: A Journey Toward Clean Code and Enjoyment
 
CucumberSeleniumWD
CucumberSeleniumWDCucumberSeleniumWD
CucumberSeleniumWD
 
AWS and Dynatrace: Moving your Cloud Strategy to the Next Level
AWS and Dynatrace: Moving your Cloud Strategy to the Next LevelAWS and Dynatrace: Moving your Cloud Strategy to the Next Level
AWS and Dynatrace: Moving your Cloud Strategy to the Next Level
 
Measuring Your Code
Measuring Your CodeMeasuring Your Code
Measuring Your Code
 
Measuring Your Code 2.0
Measuring Your Code 2.0Measuring Your Code 2.0
Measuring Your Code 2.0
 
Behavior Driven Development by Example
Behavior Driven Development by ExampleBehavior Driven Development by Example
Behavior Driven Development by Example
 
The Next Five Years of Rails
The Next Five Years of RailsThe Next Five Years of Rails
The Next Five Years of Rails
 
Cost Effective Web Application Testing
Cost Effective Web Application TestingCost Effective Web Application Testing
Cost Effective Web Application Testing
 
Cost effective web application testing
Cost effective web application testingCost effective web application testing
Cost effective web application testing
 
Cost effective web application testing
Cost effective web application testingCost effective web application testing
Cost effective web application testing
 
Resume debasish
Resume debasish Resume debasish
Resume debasish
 
Dot NET Solution Architect Roadmap By Scholarhat PDF
Dot NET Solution Architect Roadmap By Scholarhat PDFDot NET Solution Architect Roadmap By Scholarhat PDF
Dot NET Solution Architect Roadmap By Scholarhat PDF
 
Onion Architecture with S#arp
Onion Architecture with S#arpOnion Architecture with S#arp
Onion Architecture with S#arp
 
Approaching ATDD/BDD
Approaching ATDD/BDDApproaching ATDD/BDD
Approaching ATDD/BDD
 
Coding Naked
Coding NakedCoding Naked
Coding Naked
 

Recently uploaded

When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 

Recently uploaded (20)

When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 

2010-07-19_rails_tdd_week1

  • 1. Efficient Rails Test-Driven Development Wolfram Arnold www.rubyfocus.biz In collaboration with: Sarah Allen, BlazingCloud.net marakana.com
  • 2. Class Outline Week #1 The economics of testing Testing in layers, design patterns Toolbox: RSpec with Rails RSpec & Models Week #2 A culture of testing: Why TDD? How to TDD? Testing & Data Dependencies Toolbox: Fixtures, Factories, Mocks & Stubs
  • 3. Class Outline Week #3 Controller testing View, Helper, Routes Testing How much is enough? How much is too much? Week #4 Refactoring code & tests, custom matchers API Testing Remote data setup Cucumber for API testing & documentation
  • 4. Class Outline Week #5 Integration Testing—when and how? Toolbox: Cucumber, Selenium & Friends Page Object Pattern Week #6 TDD—what next? BDD, Agile Process, XP → VDD: Value-Driven Development Advanced/Special Requests
  • 5. What you can expect Presentation, Examples Answers to questions A range of material from current development practice Homework Fluidity & adaptability Fun
  • 6. It works best, when... Active participation Try something new Focus Team Effort Pairing Utilizing the resources in class: TA's, participants Discussions in public Class Q&A Mailing list
  • 8. Why “efficient” and “testing”? “Testing takes too much time.” “It's more efficient to test later.” “Testing is the responsibility of QA, not developers.” “It's not practical to test X.” Tests break when data changes. Tests break when design changes.
  • 9. The Role of Testing Development without tests... fails to empower developers to efficiently take responsibility for quality of the code delivered makes collaboration harder build narrow silos of expertise instills fear & resistance to change makes documentation a chore stops being efficient very soon
  • 10. TDD: Keeping cost of change low Cost per change without TDD with TDD Time
  • 11. Why? Non-TDD Accumulates “technical debt” unchecked Removal of technical debt carries risk The more technical debt, the higher the risk Existing technical debt attracts more technical debt Like compound interest People are most likely to do what others did before them To break the pattern heroic discipline & coordination required
  • 12. Testing in Layers Application, Browser UI Selenium RSpec Integration Test::Unit Integration Application, Server Cucumber, Webrat RSpec RSpec Views Helpers Views Helpers RSpec RSpec Test::Unit Functional Controller Routes Controller Routes RSpec Test::Unit Model Model
  • 13. Cost of Testing Layers Application, Browser UI Application, Server Views Helpers Controller Routes Model Cost
  • 14. Cost of Testing Relationship to data most Application, Browser UI removed Application, Server Views Helpers Controller Routes Model closest Cost
  • 15. Best ROI for Testing Layers Application, Browser UI Application, Server Views Helpers Controller Routes Model Impact/Line of Test Code
  • 16. TDD & Design Patterns Skinny Controller— ➢ Designed to move logic Fat Model from higher to lower DRY application layers named_scope ➢ Following design patterns makes testing Proxy Associations easier Validations ➢ Code written following ... TDD economics will naturally converge on these design patterns!
  • 17. Structure of Tests Setup Expected value Actual value Verification: actual == expected? Teardown
  • 18. Good Tests are... Compact Responsible for testing one concern only Fast DRY
  • 19. RSpec Verifications should respond_to should be_nil → works with any ? method (so-called “predicates”) should be_valid should_not be_nil; should_not be_valid lambda {...}.should change(), {}, .from().to(), .by() should eql, ==, equal
  • 20. RSpec Structure before, before(:each), before(:all) after, after(:each), after(:all) describe do...end, nested it do... end
  • 21. Let's do some coding Demo
  • 22. Models: What to test? Validation Rules Associations Any custom method Association Proxy Methods
  • 23. Let's do some coding Exercise, but wait...
  • 24. Test-First Teaching Pioneered by Sarah Allen Instructor writes tests Students write code What about writing tests?
  • 25. Behavior-First Teaching Instructor specifies desired behavior in plain English like a user story Students write tests Students write code to make tests pass
  • 26. Story Exercise #1 A Person object must have a first and last name. A Person object can construct a full name from the first and last name. A Person object has an optional middle name. A Person object returns a full name including, if present, the middle name.
  • 27. RSpec ==, eql, equal obj.should == 5 5 == 5 obj.should eql(5) 5.eql 5 obj.should equal(5) 5.equal 5 Object Equality vs. Identity Use == eql, == compare values Unless you know you need something else equal, === compare objects, classes Warning! Do not use != with RSpec. Use should_not instead.
  • 28. RSpec should change lambda { Person.create }.should change(Person, :count).by(1) lambda { @bob.addresses.create(:street => “...”) }.should change(@bob.addresses, :count).by(1) nd Warning: The 2 example is incorrect!
  • 29. RSpec should change lambda { # code that causes a change }.should change(object, :method).by(difference) or should change(object, :method).from(initial).to(final) The object typically is a class, if not, be wary of reload!
  • 30. should change with block lambda { # code that causes a change }.should change { expression }.by(difference) or should change { expression }.from(initial).to(final)
  • 31. better with block lambda { @bob.addresses.create(:street => “...”) }.should change { @bob.addresses(true).count }.by(1)
  • 32. Homework A Person should save correctly. A Person can have many Addresses. A Person's Address must have a street, city and zip. A Person's Address can have an optional country. If the country is left blank, it should default to “USA” prior to saving.
  • 33. BizConf Aug 4-6, 2010 Amelia Island, FL Discount code: WOLF Rails & Web App Professionals, Entrepreneurs, for 43% off Consultants http://bizconf.org? Small group coupon=WOLF Network with Who's Who Organized by Obie Fernandez of Hashrocket Highlight: David Allen