SlideShare a Scribd company logo
1 of 32
Download to read offline
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

More Related Content

What's hot

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
 

What's hot (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
 

Viewers also liked

Viewers also liked (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
 

Similar to 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
 

Similar to 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
 

Recently uploaded

Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
FIDO Alliance
 

Recently uploaded (20)

Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdf
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
 
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
 
Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджера
 

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