SlideShare una empresa de Scribd logo
1 de 15
BDD using RSpec
BDD vs TDD
●   Both the approach do emphasize on
    testing
●   However , in TDD you are basically
    testing the functionality of a
    function
    ●   As to what should be the output
●   In BDD we basically start specifying
    features on a top level and then
    work our way into a function
What is RSpec
●   RSpec is testing tool for the
    Ruby programming language.
●   Born under the banner of
    Behaviour-Driven Development, it
    is designed to make Test-Driven
    Development a productive and
    enjoyable experience
Rspec features
●   a rich command line program (the
    rspec command)
●   textual descriptions of examples and
    groups (rspec-core)
●   flexible and customizable reporting
●   extensible expectation language
    (rspec-expectations)
●   built-in mocking/stubbing framework
    (rspec-mocks)
Installing RSpec
●   Once you define rspec features
    you will be running the test
    suite from command line
●   In order to install rspec you
    need to install it via gem
    ●   gem install rspec
Writing features
●   Generally you start describing a
    class that you want
●   In terms of what it's behavior
    should be
●   You have a typical cycle of
    ●   Write a feature
    ●   Make it fail (Red)
    ●   Refactor the code and make it work
        (green)
describe Book do
# all features go here
end
Writing a spec
●   You start writing specs within an
    “it” block
●   Example
     it “returns a new book object” do
       b1 = Book.new
       b1.should be_an_instance_of(Book)
     end
●   be_an_instance_of is a matcher
    provided by rspec
Running tests
●   You can run a test from the
    terminal
     rspec book_spec.rb
●   If you want to see more verbose
    output on a test then
     rspec bowling_spec.rb --format
     nested
Initializers
●   If there is a certain set of
    code that you need to initialize
    for each tests then you can use
    the intializers
●   Initializing before each test
     before :each do
       @book = Book.new(:title=>
     “Learning Rails”, :author=>
     “Michael Hartman”
     end
Initializers
●   Initializing once before all
    tests
      before   :all do
       #initialization code goes here
      end
Defining a test
           subject :)
●   You can define a test subject
    which is basically the object
    that we are describing
     subject   { Article.new}
●   Once defined we can use the
    keyword “subject” to use the
    matchers on it and test it
    e.g.
    subject.should be_an_instance_of
    Article
Various Matchers
●   should
●   should_not
●   be_empty
    #equivalence check
●   eq
    #example actual.should
    eql(expected)
●   ==
    #example actual.should ==
    expected
Examples
 actual.should_not eq(expected)
 actual.should_not == expected
Comparison operators
 actual.should   be   >    expected
 actual.should   be   >=   expected
 actual.should   be   <=   expected
 actual.should   be   <    expected
Type/Class
 actual.should
 be_instance_of(expected)
 actual.should be_kind_of(expected)
Boolean check
 actual.should be_true # passes if
 actual is truthy (not nil or false)
 actual.should be_false # passes if
 actual is falsy (nil or false)
 actual.should be_nil   # passes if
 actual is nil
 actual.should be       # passes if
 actual is truthy (not nil or false)

Más contenido relacionado

La actualidad más candente

JavaScript: Patterns, Part 3
JavaScript: Patterns, Part  3JavaScript: Patterns, Part  3
JavaScript: Patterns, Part 3
Chris Farrell
 
Why should a Java programmer shifts towards Functional Programming Paradigm
Why should a Java programmer shifts towards Functional Programming ParadigmWhy should a Java programmer shifts towards Functional Programming Paradigm
Why should a Java programmer shifts towards Functional Programming Paradigm
Tech Triveni
 
Qtp training session IV
Qtp training session IVQtp training session IV
Qtp training session IV
Aisha Mazhar
 

La actualidad más candente (20)

Groovy / comparison with java
Groovy / comparison with javaGroovy / comparison with java
Groovy / comparison with java
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
DDD with Behat
DDD with BehatDDD with Behat
DDD with Behat
 
TypeScript for Java Developers
TypeScript for Java DevelopersTypeScript for Java Developers
TypeScript for Java Developers
 
Angular Unit Testing
Angular Unit TestingAngular Unit Testing
Angular Unit Testing
 
Functions in javascript
Functions in javascriptFunctions in javascript
Functions in javascript
 
Testing of React JS app
Testing of React JS appTesting of React JS app
Testing of React JS app
 
AngularJS Unit Testing
AngularJS Unit TestingAngularJS Unit Testing
AngularJS Unit Testing
 
Typescript for the programmers who like javascript
Typescript for the programmers who like javascriptTypescript for the programmers who like javascript
Typescript for the programmers who like javascript
 
Lightning talk- testing
Lightning talk- testingLightning talk- testing
Lightning talk- testing
 
Xtend - better java with -less- noise
Xtend - better java with -less- noiseXtend - better java with -less- noise
Xtend - better java with -less- noise
 
JavaScript Conditional Statements
JavaScript Conditional StatementsJavaScript Conditional Statements
JavaScript Conditional Statements
 
Karate - MoT Dallas 26-Oct-2017
Karate - MoT Dallas 26-Oct-2017Karate - MoT Dallas 26-Oct-2017
Karate - MoT Dallas 26-Oct-2017
 
JavaScript: Patterns, Part 3
JavaScript: Patterns, Part  3JavaScript: Patterns, Part  3
JavaScript: Patterns, Part 3
 
Why should a Java programmer shifts towards Functional Programming Paradigm
Why should a Java programmer shifts towards Functional Programming ParadigmWhy should a Java programmer shifts towards Functional Programming Paradigm
Why should a Java programmer shifts towards Functional Programming Paradigm
 
Qtp training session IV
Qtp training session IVQtp training session IV
Qtp training session IV
 
G pars
G parsG pars
G pars
 
F
FF
F
 
Introduction To JavaScript
Introduction To JavaScriptIntroduction To JavaScript
Introduction To JavaScript
 
Karate for Complex Web-Service API Testing by Peter Thomas
Karate for Complex Web-Service API Testing by Peter ThomasKarate for Complex Web-Service API Testing by Peter Thomas
Karate for Complex Web-Service API Testing by Peter Thomas
 

Destacado

Neuro Cognitive Platform
Neuro Cognitive PlatformNeuro Cognitive Platform
Neuro Cognitive Platform
Edgar Grant
 
Classroom observationchecklistform
Classroom observationchecklistformClassroom observationchecklistform
Classroom observationchecklistform
Javier Cortez
 
Movable Type 案内資料
Movable Type 案内資料Movable Type 案内資料
Movable Type 案内資料
Six Apart KK
 

Destacado (13)

Neuro Cognitive Platform
Neuro Cognitive PlatformNeuro Cognitive Platform
Neuro Cognitive Platform
 
Definición de riesgos financieros
Definición de riesgos financierosDefinición de riesgos financieros
Definición de riesgos financieros
 
Concours d'idées de la Société Immobilière de Québec
Concours d'idées de la Société Immobilière de QuébecConcours d'idées de la Société Immobilière de Québec
Concours d'idées de la Société Immobilière de Québec
 
Wall e el robot limpia ventanas
Wall e el robot limpia ventanasWall e el robot limpia ventanas
Wall e el robot limpia ventanas
 
Classroom observationchecklistform
Classroom observationchecklistformClassroom observationchecklistform
Classroom observationchecklistform
 
The Curious Crate concept
The Curious Crate conceptThe Curious Crate concept
The Curious Crate concept
 
EL PROCESO DE RESOLUCION
EL PROCESO DE RESOLUCIONEL PROCESO DE RESOLUCION
EL PROCESO DE RESOLUCION
 
Ruta investigativa
Ruta investigativaRuta investigativa
Ruta investigativa
 
Adolf Hitler: Evil's greatest Emissary
Adolf Hitler: Evil's greatest EmissaryAdolf Hitler: Evil's greatest Emissary
Adolf Hitler: Evil's greatest Emissary
 
A starbucks wedding!
A starbucks wedding!A starbucks wedding!
A starbucks wedding!
 
Пітчінг стартапів IoT HUB Kiev
Пітчінг стартапів IoT HUB KievПітчінг стартапів IoT HUB Kiev
Пітчінг стартапів IoT HUB Kiev
 
Ніхто не забутий
Ніхто не забутийНіхто не забутий
Ніхто не забутий
 
Movable Type 案内資料
Movable Type 案内資料Movable Type 案内資料
Movable Type 案内資料
 

Similar a Ruby on rails rspec

Unit testing Ch. 13 of Programming Ruby
Unit testing Ch. 13 of Programming RubyUnit testing Ch. 13 of Programming Ruby
Unit testing Ch. 13 of Programming Ruby
lvrubygroup
 
BDD style Unit Testing
BDD style Unit TestingBDD style Unit Testing
BDD style Unit Testing
Wen-Tien Chang
 

Similar a Ruby on rails rspec (20)

Rspec
RspecRspec
Rspec
 
Unit testing Ch. 13 of Programming Ruby
Unit testing Ch. 13 of Programming RubyUnit testing Ch. 13 of Programming Ruby
Unit testing Ch. 13 of Programming Ruby
 
BDD style Unit Testing
BDD style Unit TestingBDD style Unit Testing
BDD style Unit Testing
 
Testing Spark and Scala
Testing Spark and ScalaTesting Spark and Scala
Testing Spark and Scala
 
Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013
 
TDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and GomegaTDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and Gomega
 
8 introduction to_java_script
8 introduction to_java_script8 introduction to_java_script
8 introduction to_java_script
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming language
 
Puppet Camp Paris 2014: Test Driven Development
Puppet Camp Paris 2014: Test Driven DevelopmentPuppet Camp Paris 2014: Test Driven Development
Puppet Camp Paris 2014: Test Driven Development
 
20140408 tdd puppetcamp-paris
20140408 tdd puppetcamp-paris20140408 tdd puppetcamp-paris
20140408 tdd puppetcamp-paris
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Ansible, integration testing, and you.
Ansible, integration testing, and you.Ansible, integration testing, and you.
Ansible, integration testing, and you.
 
Rspec tutorial
Rspec tutorialRspec tutorial
Rspec tutorial
 
Tips to make your rspec specs awesome
Tips to make your rspec specs awesomeTips to make your rspec specs awesome
Tips to make your rspec specs awesome
 
2011-02-03 LA RubyConf Rails3 TDD Workshop
2011-02-03 LA RubyConf Rails3 TDD Workshop2011-02-03 LA RubyConf Rails3 TDD Workshop
2011-02-03 LA RubyConf Rails3 TDD Workshop
 
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
 
TDD, BDD, RSpec
TDD, BDD, RSpecTDD, BDD, RSpec
TDD, BDD, RSpec
 
RubyTesting
RubyTestingRubyTesting
RubyTesting
 
RubyTesting
RubyTestingRubyTesting
RubyTesting
 
RSpec 3.0: Under the Covers
RSpec 3.0: Under the CoversRSpec 3.0: Under the Covers
RSpec 3.0: Under the Covers
 

Ruby on rails rspec

  • 2. BDD vs TDD ● Both the approach do emphasize on testing ● However , in TDD you are basically testing the functionality of a function ● As to what should be the output ● In BDD we basically start specifying features on a top level and then work our way into a function
  • 3. What is RSpec ● RSpec is testing tool for the Ruby programming language. ● Born under the banner of Behaviour-Driven Development, it is designed to make Test-Driven Development a productive and enjoyable experience
  • 4. Rspec features ● a rich command line program (the rspec command) ● textual descriptions of examples and groups (rspec-core) ● flexible and customizable reporting ● extensible expectation language (rspec-expectations) ● built-in mocking/stubbing framework (rspec-mocks)
  • 5. Installing RSpec ● Once you define rspec features you will be running the test suite from command line ● In order to install rspec you need to install it via gem ● gem install rspec
  • 6. Writing features ● Generally you start describing a class that you want ● In terms of what it's behavior should be ● You have a typical cycle of ● Write a feature ● Make it fail (Red) ● Refactor the code and make it work (green)
  • 7. describe Book do # all features go here end
  • 8. Writing a spec ● You start writing specs within an “it” block ● Example it “returns a new book object” do b1 = Book.new b1.should be_an_instance_of(Book) end ● be_an_instance_of is a matcher provided by rspec
  • 9. Running tests ● You can run a test from the terminal rspec book_spec.rb ● If you want to see more verbose output on a test then rspec bowling_spec.rb --format nested
  • 10. Initializers ● If there is a certain set of code that you need to initialize for each tests then you can use the intializers ● Initializing before each test before :each do @book = Book.new(:title=> “Learning Rails”, :author=> “Michael Hartman” end
  • 11. Initializers ● Initializing once before all tests before :all do #initialization code goes here end
  • 12. Defining a test subject :) ● You can define a test subject which is basically the object that we are describing subject { Article.new} ● Once defined we can use the keyword “subject” to use the matchers on it and test it e.g. subject.should be_an_instance_of Article
  • 13. Various Matchers ● should ● should_not ● be_empty #equivalence check ● eq #example actual.should eql(expected) ● == #example actual.should == expected
  • 14. Examples actual.should_not eq(expected) actual.should_not == expected Comparison operators actual.should be > expected actual.should be >= expected actual.should be <= expected actual.should be < expected Type/Class actual.should be_instance_of(expected) actual.should be_kind_of(expected)
  • 15. Boolean check actual.should be_true # passes if actual is truthy (not nil or false) actual.should be_false # passes if actual is falsy (nil or false) actual.should be_nil # passes if actual is nil actual.should be # passes if actual is truthy (not nil or false)