SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Downloaden Sie, um offline zu lesen
RSpec
Freelancer


•   http://simplabs.com

•   http://xing.com/profile/Marco_OtteWitte

•   http://github.com/marcoow
Buzzwords

  TDD
Buzzwords

  X
  TDD
Buzzwords

 XTDD

BDD
BDD
BDD
Beschreiben von Verhalten
BDD
Beschreiben von Verhalten

mit natürlicher Sprache bzw.
Domänen- Sprache
BDD
Beschreiben von Verhalten

mit natürlicher Sprache bzw.
Domänen- Sprache

Spezifikation und
(Regressions)- Tests in einem
1 class TaskTest < ActiveSupport::TestCase
2
3   test 'Test::Unit is rocking' do
4     assert Test::Unit.rocking?
5   end
6
7 end
1 describe RSpec do
2
3   it 'should be rocking' do
4     RSpec.should be_rocking
5   end
6
7 end
Syntactic Sugar
RSpec.should be_rocking

=>

RSpec.rocking?
Basics
 1 describe Something do
 2
 3   before do
 4     @thing = Something.new
 5   end
 6
 7   it 'should behave in some way' do
 8     @thing.action.should == 'expected'
 9   end
10
11   after(:all) do
12     Something.destroy_all
13   end
14
15 end
Model Specs
 1 describe Task do
 2
 3   describe 'validation' do
 4
 5     before do
 6       @task = Task.new(:title => 'test task')
 7     end
 8
 9     it 'should succeed when all attributres are set' do
10       @task.should be_valid
11     end
12
13   end
14
15 end
Controller Specs
 1 describe TasksController do
 2
 3   describe 'handling GET /tasks' do
 4
 5     it quot;should render the 'tasks/index' templatequot; do
 6       get :index
 7
 8       response.should render_template('tasks/index')
 9     end
10
12   end
13
14 end
View Specs
 1 describe 'tasks/index' do
 2
 3   before do
 4     @task = stub_model(Task)
 5     assigns[:tasks] = [@task]
 6   end
 7
 8   it 'should render a link to tasks/new' do
 9     render('tasks/index')
10
11     response.should have_tag('a[href=?]', new_task_path)
12   end
13
14 end
Demo
Shared Examples (1)
 1 describe 'a secure action', :shared => true do
 2
 3   describe 'without a logged-in user' do
 4
 5     it 'should redirect to new_session_path' do
 6       do_request
 7
 8       response.should redirect_to(new_session_path)
 9     end
10
11   end
12
13 end
Shared Examples (2)
 1 describe TasksController do
 2
 3   describe 'handling GET /tasks' do
 4
 5     it_should_behave_like 'a secure action'
 6
 7     # => redirect without logged-in user
 8
 9   end
10
11 end
Demo
Matchers
 1 class RequireAttributeMatcher
 2
 3     def initialize(attribute)
 4       @attribute = attribute
 5     end
 6
 7     def matches?(model)
 8       @model = model
 9       model.send(quot;#{@attribute.to_s}=quot;.to_sym, nil)
10       return !model.valid?
11     end
12
13   end
Demo
Stories (Cucumber)

Given [Context]
When   [Aktion]
Then   [Business Value!]
Stories (Cucumber)
Scenario: Create Task
Given that there are no tasks
When I create task quot;task 1quot;
When I go to the tasks page
Then I should see quot;task 1quot;
Stories (Cucumber)
 1   Given /^that there are no tasks$/ do
 2     Task.destroy_all
 3   end
 4
 5   When /^I create task quot;(.*)quot;$/ do |title|
 6     post tasks_url, :task => { :title => title }
 7   end
 8
 9   When /^I go to the tasks page$/ do
10     get tasks_url
11   end
12
13   Then /^I should see quot;(.*)quot;$/ do |title|
14     response.body.should =~ /#{title}/m
15   end
Demo
Installation

ruby script/plugin install git://github.com/
dchelimsky/rspec.git

ruby script/plugin install git://github.com/
dchelimsky/rspec-rails.git

ruby script/generate rspec
Autotest

sudo gem install ZenTest

RSPEC=true autotest

RSPEC=true AUTOFEATURE=true autotest
Demo
Report
[spec/rspec.opts]

--format html:doc/spec/report.html
Q&A
Ressourcen
•   http://rspec.simplabs.com

•   http://rspec.info/

•   http://github.com/dchelimsky/rspec

•   http://github.com/dchelimsky/rspec-rails

•   http://github.com/aslakhellesoy/cucumber

•   http://dannorth.net/introducing-bdd

Weitere ähnliche Inhalte

Was ist angesagt?

Java script object model
Java script object modelJava script object model
Java script object model
James Hsieh
 
React.js or why DOM finally makes sense
React.js or why DOM finally makes senseReact.js or why DOM finally makes sense
React.js or why DOM finally makes sense
Eldar Djafarov
 
Unit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and NodeUnit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and Node
Josh Mock
 
I18n
I18nI18n
I18n
soon
 

Was ist angesagt? (20)

Java script object model
Java script object modelJava script object model
Java script object model
 
Workshop 5: JavaScript testing
Workshop 5: JavaScript testingWorkshop 5: JavaScript testing
Workshop 5: JavaScript testing
 
Getting to Grips with SilverStripe Testing
Getting to Grips with SilverStripe TestingGetting to Grips with SilverStripe Testing
Getting to Grips with SilverStripe Testing
 
SilverStripe CMS JavaScript Refactoring
SilverStripe CMS JavaScript RefactoringSilverStripe CMS JavaScript Refactoring
SilverStripe CMS JavaScript Refactoring
 
React.js or why DOM finally makes sense
React.js or why DOM finally makes senseReact.js or why DOM finally makes sense
React.js or why DOM finally makes sense
 
Angular server-side communication
Angular server-side communicationAngular server-side communication
Angular server-side communication
 
Unit testing with mocha
Unit testing with mochaUnit testing with mocha
Unit testing with mocha
 
Unit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and NodeUnit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and Node
 
Workshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte IIIWorkshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte III
 
Introduction to Redux
Introduction to ReduxIntroduction to Redux
Introduction to Redux
 
Testing Ruby with Rspec (a beginner's guide)
Testing Ruby with Rspec (a beginner's guide)Testing Ruby with Rspec (a beginner's guide)
Testing Ruby with Rspec (a beginner's guide)
 
I18n
I18nI18n
I18n
 
Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6
 
Python for AngularJS
Python for AngularJSPython for AngularJS
Python for AngularJS
 
Optimizing a large angular application (ng conf)
Optimizing a large angular application (ng conf)Optimizing a large angular application (ng conf)
Optimizing a large angular application (ng conf)
 
Integrating React.js with PHP projects
Integrating React.js with PHP projectsIntegrating React.js with PHP projects
Integrating React.js with PHP projects
 
정오의 데이트 for iOS 코드 정리
정오의 데이트 for iOS 코드 정리정오의 데이트 for iOS 코드 정리
정오의 데이트 for iOS 코드 정리
 
JavaScript Unit Testing with Jasmine
JavaScript Unit Testing with JasmineJavaScript Unit Testing with Jasmine
JavaScript Unit Testing with Jasmine
 
Angular2 & ngrx/store: Game of States
Angular2 & ngrx/store: Game of StatesAngular2 & ngrx/store: Game of States
Angular2 & ngrx/store: Game of States
 
Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testing
 

Andere mochten auch

Andere mochten auch (13)

Getting Answers to Your Testing Questions
Getting Answers to Your Testing QuestionsGetting Answers to Your Testing Questions
Getting Answers to Your Testing Questions
 
Straight Up RSpec
Straight Up RSpecStraight Up RSpec
Straight Up RSpec
 
Wrong
WrongWrong
Wrong
 
TDD with RSpec
TDD with RSpecTDD with RSpec
TDD with RSpec
 
RSpec 3.0: Under the Covers
RSpec 3.0: Under the CoversRSpec 3.0: Under the Covers
RSpec 3.0: Under the Covers
 
RSpec: What, How and Why
RSpec: What, How and WhyRSpec: What, How and Why
RSpec: What, How and Why
 
Rspec 101
Rspec 101Rspec 101
Rspec 101
 
Tdd with rspec.md
Tdd with rspec.mdTdd with rspec.md
Tdd with rspec.md
 
Straight Up RSpec 3 - a neat Ruby BDD tool
Straight Up RSpec 3 - a neat Ruby BDD toolStraight Up RSpec 3 - a neat Ruby BDD tool
Straight Up RSpec 3 - a neat Ruby BDD tool
 
Test Driven
Test DrivenTest Driven
Test Driven
 
RSpec. Part 3
RSpec. Part 3RSpec. Part 3
RSpec. Part 3
 
The WHY behind TDD/BDD and the HOW with RSpec
The WHY behind TDD/BDD and the HOW with RSpecThe WHY behind TDD/BDD and the HOW with RSpec
The WHY behind TDD/BDD and the HOW with RSpec
 
RSpec 3: The new, the old, the good
RSpec 3: The new, the old, the goodRSpec 3: The new, the old, the good
RSpec 3: The new, the old, the good
 

Ähnlich wie RSpec

Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code styleRuby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Anton Shemerey
 
A Z Introduction To Ruby On Rails
A Z Introduction To Ruby On RailsA Z Introduction To Ruby On Rails
A Z Introduction To Ruby On Rails
railsconf
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Paulo Ragonha
 

Ähnlich wie RSpec (20)

How To Test Everything
How To Test EverythingHow To Test Everything
How To Test Everything
 
TDD & BDD
TDD & BDDTDD & BDD
TDD & BDD
 
Rspec
RspecRspec
Rspec
 
Why ruby
Why rubyWhy ruby
Why ruby
 
Ruby on Rails testing with Rspec
Ruby on Rails testing with RspecRuby on Rails testing with Rspec
Ruby on Rails testing with Rspec
 
Migrating legacy data
Migrating legacy dataMigrating legacy data
Migrating legacy data
 
ES6 Simplified
ES6 SimplifiedES6 Simplified
ES6 Simplified
 
Introduction to unit testing
Introduction to unit testingIntroduction to unit testing
Introduction to unit testing
 
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code styleRuby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
 
Ruby Metaprogramming
Ruby MetaprogrammingRuby Metaprogramming
Ruby Metaprogramming
 
Una Critica a Rails by Luca Guidi
Una Critica a Rails by Luca GuidiUna Critica a Rails by Luca Guidi
Una Critica a Rails by Luca Guidi
 
Blocks by Lachs Cox
Blocks by Lachs CoxBlocks by Lachs Cox
Blocks by Lachs Cox
 
Testing Legacy Rails Apps
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails Apps
 
A Z Introduction To Ruby On Rails
A Z Introduction To Ruby On RailsA Z Introduction To Ruby On Rails
A Z Introduction To Ruby On Rails
 
A-Z Intro To Rails
A-Z Intro To RailsA-Z Intro To Rails
A-Z Intro To Rails
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Refatoração + Design Patterns em Ruby
Refatoração + Design Patterns em RubyRefatoração + Design Patterns em Ruby
Refatoração + Design Patterns em Ruby
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Rails
 
Well
WellWell
Well
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
 

Kürzlich hochgeladen

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Kürzlich hochgeladen (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

RSpec

  • 2. Freelancer • http://simplabs.com • http://xing.com/profile/Marco_OtteWitte • http://github.com/marcoow
  • 6. BDD
  • 8. BDD Beschreiben von Verhalten mit natürlicher Sprache bzw. Domänen- Sprache
  • 9. BDD Beschreiben von Verhalten mit natürlicher Sprache bzw. Domänen- Sprache Spezifikation und (Regressions)- Tests in einem
  • 10. 1 class TaskTest < ActiveSupport::TestCase 2 3 test 'Test::Unit is rocking' do 4 assert Test::Unit.rocking? 5 end 6 7 end
  • 11. 1 describe RSpec do 2 3 it 'should be rocking' do 4 RSpec.should be_rocking 5 end 6 7 end
  • 13. Basics 1 describe Something do 2 3 before do 4 @thing = Something.new 5 end 6 7 it 'should behave in some way' do 8 @thing.action.should == 'expected' 9 end 10 11 after(:all) do 12 Something.destroy_all 13 end 14 15 end
  • 14. Model Specs 1 describe Task do 2 3 describe 'validation' do 4 5 before do 6 @task = Task.new(:title => 'test task') 7 end 8 9 it 'should succeed when all attributres are set' do 10 @task.should be_valid 11 end 12 13 end 14 15 end
  • 15. Controller Specs 1 describe TasksController do 2 3 describe 'handling GET /tasks' do 4 5 it quot;should render the 'tasks/index' templatequot; do 6 get :index 7 8 response.should render_template('tasks/index') 9 end 10 12 end 13 14 end
  • 16. View Specs 1 describe 'tasks/index' do 2 3 before do 4 @task = stub_model(Task) 5 assigns[:tasks] = [@task] 6 end 7 8 it 'should render a link to tasks/new' do 9 render('tasks/index') 10 11 response.should have_tag('a[href=?]', new_task_path) 12 end 13 14 end
  • 17. Demo
  • 18. Shared Examples (1) 1 describe 'a secure action', :shared => true do 2 3 describe 'without a logged-in user' do 4 5 it 'should redirect to new_session_path' do 6 do_request 7 8 response.should redirect_to(new_session_path) 9 end 10 11 end 12 13 end
  • 19. Shared Examples (2) 1 describe TasksController do 2 3 describe 'handling GET /tasks' do 4 5 it_should_behave_like 'a secure action' 6 7 # => redirect without logged-in user 8 9 end 10 11 end
  • 20. Demo
  • 21. Matchers 1 class RequireAttributeMatcher 2 3 def initialize(attribute) 4 @attribute = attribute 5 end 6 7 def matches?(model) 8 @model = model 9 model.send(quot;#{@attribute.to_s}=quot;.to_sym, nil) 10 return !model.valid? 11 end 12 13 end
  • 22. Demo
  • 23. Stories (Cucumber) Given [Context] When [Aktion] Then [Business Value!]
  • 24. Stories (Cucumber) Scenario: Create Task Given that there are no tasks When I create task quot;task 1quot; When I go to the tasks page Then I should see quot;task 1quot;
  • 25. Stories (Cucumber) 1 Given /^that there are no tasks$/ do 2 Task.destroy_all 3 end 4 5 When /^I create task quot;(.*)quot;$/ do |title| 6 post tasks_url, :task => { :title => title } 7 end 8 9 When /^I go to the tasks page$/ do 10 get tasks_url 11 end 12 13 Then /^I should see quot;(.*)quot;$/ do |title| 14 response.body.should =~ /#{title}/m 15 end
  • 26. Demo
  • 27. Installation ruby script/plugin install git://github.com/ dchelimsky/rspec.git ruby script/plugin install git://github.com/ dchelimsky/rspec-rails.git ruby script/generate rspec
  • 28. Autotest sudo gem install ZenTest RSPEC=true autotest RSPEC=true AUTOFEATURE=true autotest
  • 29. Demo
  • 31. Q&A
  • 32. Ressourcen • http://rspec.simplabs.com • http://rspec.info/ • http://github.com/dchelimsky/rspec • http://github.com/dchelimsky/rspec-rails • http://github.com/aslakhellesoy/cucumber • http://dannorth.net/introducing-bdd