SlideShare a Scribd company logo
1 of 49
Download to read offline
The Basic of RSpec 2
Authors: Triet Le – Truc Nguyen
Agenda
•TDD, BDD intro
•RSpec intro
•Expectation - Matcher
•Mocking-Stubing
•RSpec Rails
•Controller Specs
•Model Specs
•Rspec vs Cucumber
•Feature Specs
•Code coverage tool: SimpleCov, Rcov
•Example Code
Test Driven Development
Source: http://centricconsulting.com/agile-test-driven-development/
•It's 'tests' that 'drive' the 'development'
•Make sure that No code goes into production
without associated tests
•Benefits of TDD:
http://agilepainrelief.com/notesfromatooluser/2
008/10/advantages-of-tdd.html
TDD
Behavior Driven Development
BDD
•BDD is based on TDD
•BDD is specifying how your application should
work, rather than verifying that it works.
•Behaviour-Driven Development is about
implementing an application by describing its
behavior from the perspective of its
stakeholders. (Rspec book)
RSpec?
RSpec
•Rspec is unit-testing framework for Ruby
programming language
•RSpec is BDD
•Rspec's strongly recommended with TDD
How
describe `Class` do
before do
# Setup something
end
it “should return something“ do
# actual_result.should matcher(expected_result)
end
end
HOW
An Example
E
x
a
m
p
l
e
g
r
o
u
p
This is a Spec file
Expectation - Matcher
http://rubydoc.info/gems/rspec-expectations/
frames
Expectation - Matcher
•Basic structure of an rspec expectation
o Old syntax
 actual.should matcher(expected)
 actual.should_not matcher(expected)
o New
 expect(actual).to eq(expected)
 expect(actual).not_to eq(expected)
•For example
o 5.should eq(5) / expect(5).to eq(5)
o 5.should_not eq(4) / expect(5).not_to eq(5)
For Example:
Mocking - Stubbing
http://rubydoc.info/gems/rspec-mocks/frames
Mocking - Stubbing
Rspec-mocks is a test-double framework for
rspec with support for methods
o mock a `fake` object
o stubs
o message expectations on generated test-doubles and
real objects alike.
Mocking - Stubbing
•Test double
o book = double("book")
•Method Stubs
o book.stub(:title) { "The RSpec Book" }
o book.stub(:title => "The RSpec Book")
o book.stub(:title).and_return("The RSpec Book")
•Message expectations
o person = double("person")
o Person.should_receive(:find) { person }
•should_receive vs stub
Mocking - Stubbing
•Good for
o Speed up testing
o Real object is unavailable
o Difficult to access from a test environment: External
services
o Queries with complicated data setup
Mocking - Stubbing
•Problems
o Simulated API gets out of sync with actual API
o Leads to testing implementation, not effect
o Demands on integration and exploratory testing higher
with mocks
o Less value per line of test code!
RSpec-Rails
https://www.relishapp.com/rspec/rspec-rails/docs
Rspec-rails
•Add to Gemfile
•group :test, :development do
gem "rspec-rails", "~> 2.4"end
•Recommended
oFactory-girl
oGuard-spec
oSpork
o SimpleCov
Rspec-rails
source: http://www.rubyfocus.biz/
Views
Controller Routes
Application, Browser UI
Application, Server
Helpers
Model
Selenium
RSpec Integration/Request,
Cucumber, Webrat, Capybara
RSpec Views RSpec Helpers
RSpec
Controller
RSpec Routing
RSpec Model Test::Unit
Test::Unit
Functional
Test::Unit
Integration
Application, Browser UI
Application, Server
https://www.relishapp.com/rspec/rspec-rails/
v/2-13/docs/controller-specs
Controller Specs
Controller specs
•Simulate a single HTTP verb in each example
o GET
o POST
o PUT
o DELETE
o XHR
•Accessable variables
o controller, request, response
o assigns, cookies, flash, and session
Controller specs
•Check rendering
oCorrect template
 response.should render_template
oRedirect
 response.should redirect_to (url or hash)
o Status code
 response.code.should eq(200)
•Verify variable assignments
o Instance variables assigned in the controller to be
shared with the view
o Cookies sent back with the response
 cookies['key']
 cookies['key']
What need to test in model?
Model Specs
Model specs
•Exists attributes
•Association
•Model’s validations
•Class methods
•Instance methods
Model specs
For detail, a model spec should include:
•Attributes
o model attributes should have
•Association
o model association should have
•The model’s create method -> check the
validation work?
o passed valid attributes => should be valid.
o fail validations => should not be valid.
•Class and instance methods perform as
expected
Model specs - Example code
code example
Model specs - Example rspec model
code
fields, association,
validations
The model’s method create
class and
instance method
Same and difference
Rspec vs Cucumber
Rspec vs Cucumber
•Both testing frameworks.
•Both are used for Acceptance Testing
•These are business-case driven Integration
Tests
o simulate the way a user uses the application,
o the way the different parts of your application work
together can be found in a way that unit testing will not
find.
same
Rspec vs Cucumber
•RSpec and Cucumber are the business
readability factor
difference
CU CU M B ER
odraw is that the
specification (features)
from the test code
oproduct owners can
provide or review without
having to dig through code
R SPEC
odescribe a step with a
Describe, Context or It
block that contains the
business specification
oa little easier for
developers to work
obut a little harder for
non-technical folks
Example
Integration test with rspec and capybara
(and Senelium)
Feature Specs
•Introduce
•Setup env
•Example code
Feature Specs - Introduce
•high-level tests meant to exercise slices of
functionality through an application. Drive the
application only via its external interface,
usually web pages.
•Require the capybara gem, version 2.0.0 or
later. Refer to the capybara API for more infos
on the methods and matchers
•Feature, scenario, background,
given DSL <=>describe, it, before
each, let. Alias methods that allow to read
more as customer tests and acceptance tests.
Feature Specs - Setup env
First, add Capybara to your
Gemfile:
In spec/spec_helper.rb,
add two require calls for
Capybara near the top
Capybara’s DSL will be
available spec/requests
and spec/integration
directory
Feature Specs - Selenium
First, add Capybara to your
Gemfile:
In spec/spec_helper.rb,
add two require calls for
Capybara near the top
•Run Selenium, just set :js => true
Firefox should
automatically
fire up and run
with Selenium.
•Capybara.default_driver = :selenium (make
Capybara to all your tests - don’t recommend)
•Using Rack::Test (default driver) and Selenium
(JS driver) by setting the :js attribute (faster if use
Selenium for tests that actually require JS)
Feature Specs - Sample code
When writing integration tests, try to model the test around an actor (user of the system) and
the action they are performing.
Feature Specs - Sample code
code coverage tool
SimpleCov, Rcov
Demo
•Model spec
•Feature specs
Thank You!
•New syntax expectation rspec
•Feature specs
•Rspec vs cucumber
•http://www.slideshare.net/NasceniaIT/tdd-bdd-r-spec
•Intergartion test
•Setup capypara
•Devise with rspec
References

More Related Content

What's hot

Web a Quebec - JS Debugging
Web a Quebec - JS DebuggingWeb a Quebec - JS Debugging
Web a Quebec - JS DebuggingRami Sayar
 
Java 8 New features
Java 8 New featuresJava 8 New features
Java 8 New featuresSon Nguyen
 
Parsing and Rewriting Ruby Templates
Parsing and Rewriting Ruby TemplatesParsing and Rewriting Ruby Templates
Parsing and Rewriting Ruby TemplatesJohn Hawthorn
 
Apikit from command line
Apikit from command lineApikit from command line
Apikit from command linefedefortin
 
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...Caktus Group
 
Integration Testing With Cucumber How To Test Anything J A O O 2009
Integration Testing With  Cucumber    How To Test Anything    J A O O 2009Integration Testing With  Cucumber    How To Test Anything    J A O O 2009
Integration Testing With Cucumber How To Test Anything J A O O 2009Dr Nic Williams
 
All Aboard for Laravel 5.1
All Aboard for Laravel 5.1All Aboard for Laravel 5.1
All Aboard for Laravel 5.1Jason McCreary
 
What's New in Laravel 5 (Laravel Meetup - 23th Apr 15, Yogyakarta, ID)
What's New in Laravel 5 (Laravel Meetup - 23th Apr 15, Yogyakarta, ID)What's New in Laravel 5 (Laravel Meetup - 23th Apr 15, Yogyakarta, ID)
What's New in Laravel 5 (Laravel Meetup - 23th Apr 15, Yogyakarta, ID)Roes Wibowo
 
Angular JS in 2017
Angular JS in 2017Angular JS in 2017
Angular JS in 2017Ayush Sharma
 
Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)Viral Solani
 
Knowing Laravel 5 : The most popular PHP framework
Knowing Laravel 5 : The most popular PHP frameworkKnowing Laravel 5 : The most popular PHP framework
Knowing Laravel 5 : The most popular PHP frameworkBukhori Aqid
 
REST Easy with Django-Rest-Framework
REST Easy with Django-Rest-FrameworkREST Easy with Django-Rest-Framework
REST Easy with Django-Rest-FrameworkMarcel Chastain
 
SCR Annotations for Fun and Profit
SCR Annotations for Fun and ProfitSCR Annotations for Fun and Profit
SCR Annotations for Fun and ProfitMike Pfaff
 
Building a REST Service in minutes with Spring Boot
Building a REST Service in minutes with Spring BootBuilding a REST Service in minutes with Spring Boot
Building a REST Service in minutes with Spring BootOmri Spector
 
A Tour of PostgREST
A Tour of PostgRESTA Tour of PostgREST
A Tour of PostgRESTbegriffs
 

What's hot (20)

Rails Performance
Rails PerformanceRails Performance
Rails Performance
 
Web a Quebec - JS Debugging
Web a Quebec - JS DebuggingWeb a Quebec - JS Debugging
Web a Quebec - JS Debugging
 
Java 8 New features
Java 8 New featuresJava 8 New features
Java 8 New features
 
Parsing and Rewriting Ruby Templates
Parsing and Rewriting Ruby TemplatesParsing and Rewriting Ruby Templates
Parsing and Rewriting Ruby Templates
 
RoR guide_p1
RoR guide_p1RoR guide_p1
RoR guide_p1
 
Apikit from command line
Apikit from command lineApikit from command line
Apikit from command line
 
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
 
Rspec
RspecRspec
Rspec
 
Integration Testing With Cucumber How To Test Anything J A O O 2009
Integration Testing With  Cucumber    How To Test Anything    J A O O 2009Integration Testing With  Cucumber    How To Test Anything    J A O O 2009
Integration Testing With Cucumber How To Test Anything J A O O 2009
 
Selenium with protractor
Selenium with protractorSelenium with protractor
Selenium with protractor
 
All Aboard for Laravel 5.1
All Aboard for Laravel 5.1All Aboard for Laravel 5.1
All Aboard for Laravel 5.1
 
What's New in Laravel 5 (Laravel Meetup - 23th Apr 15, Yogyakarta, ID)
What's New in Laravel 5 (Laravel Meetup - 23th Apr 15, Yogyakarta, ID)What's New in Laravel 5 (Laravel Meetup - 23th Apr 15, Yogyakarta, ID)
What's New in Laravel 5 (Laravel Meetup - 23th Apr 15, Yogyakarta, ID)
 
Angular JS in 2017
Angular JS in 2017Angular JS in 2017
Angular JS in 2017
 
Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)
 
Knowing Laravel 5 : The most popular PHP framework
Knowing Laravel 5 : The most popular PHP frameworkKnowing Laravel 5 : The most popular PHP framework
Knowing Laravel 5 : The most popular PHP framework
 
REST Easy with Django-Rest-Framework
REST Easy with Django-Rest-FrameworkREST Easy with Django-Rest-Framework
REST Easy with Django-Rest-Framework
 
SCR Annotations for Fun and Profit
SCR Annotations for Fun and ProfitSCR Annotations for Fun and Profit
SCR Annotations for Fun and Profit
 
ASP.NET MVC
ASP.NET MVCASP.NET MVC
ASP.NET MVC
 
Building a REST Service in minutes with Spring Boot
Building a REST Service in minutes with Spring BootBuilding a REST Service in minutes with Spring Boot
Building a REST Service in minutes with Spring Boot
 
A Tour of PostgREST
A Tour of PostgRESTA Tour of PostgREST
A Tour of PostgREST
 

Viewers also liked

Automated testing with RSpec
Automated testing with RSpecAutomated testing with RSpec
Automated testing with RSpecNascenia IT
 
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)Vysakh Sreenivasan
 
RSpec: What, How and Why
RSpec: What, How and WhyRSpec: What, How and Why
RSpec: What, How and WhyRatan Sebastian
 
Railsで春から始めるtdd生活
Railsで春から始めるtdd生活Railsで春から始めるtdd生活
Railsで春から始めるtdd生活Yamamoto Kazuhisa
 
Ruby on Rails testing with Rspec
Ruby on Rails testing with RspecRuby on Rails testing with Rspec
Ruby on Rails testing with RspecBunlong Van
 
BDD style Unit Testing
BDD style Unit TestingBDD style Unit Testing
BDD style Unit TestingWen-Tien Chang
 
Ruby On Rails: Web-разработка по-другому!
Ruby On Rails: Web-разработка по-другому!Ruby On Rails: Web-разработка по-другому!
Ruby On Rails: Web-разработка по-другому!Constantin Kichinsky
 

Viewers also liked (10)

Automated testing with RSpec
Automated testing with RSpecAutomated testing with RSpec
Automated testing with RSpec
 
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)
 
RSpec: What, How and Why
RSpec: What, How and WhyRSpec: What, How and Why
RSpec: What, How and Why
 
MacRuby
MacRubyMacRuby
MacRuby
 
Railsで春から始めるtdd生活
Railsで春から始めるtdd生活Railsで春から始めるtdd生活
Railsで春から始めるtdd生活
 
Ruby on Rails testing with Rspec
Ruby on Rails testing with RspecRuby on Rails testing with Rspec
Ruby on Rails testing with Rspec
 
Rspec 101
Rspec 101Rspec 101
Rspec 101
 
BDD style Unit Testing
BDD style Unit TestingBDD style Unit Testing
BDD style Unit Testing
 
Ruby On Rails: Web-разработка по-другому!
Ruby On Rails: Web-разработка по-другому!Ruby On Rails: Web-разработка по-другому!
Ruby On Rails: Web-разработка по-другому!
 
RSpec 2 Best practices
RSpec 2 Best practicesRSpec 2 Best practices
RSpec 2 Best practices
 

Similar to Basic RSpec 2

Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
       Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber       Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & CucumberUdaya Kiran
 
Capybara and cucumber with DSL using ruby
Capybara and cucumber with DSL using rubyCapybara and cucumber with DSL using ruby
Capybara and cucumber with DSL using rubyDeepak Chandella
 
Rails automatic test driven development
Rails automatic test driven developmentRails automatic test driven development
Rails automatic test driven developmenttyler4long
 
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 2013Brian Sam-Bodden
 
Rethinking Testing
Rethinking TestingRethinking Testing
Rethinking Testingpdejuan
 
Robotframework
RobotframeworkRobotframework
RobotframeworkElla Sun
 
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 WorkshopWolfram Arnold
 
Ruby on Rails Penetration Testing
Ruby on Rails Penetration TestingRuby on Rails Penetration Testing
Ruby on Rails Penetration Testing3S Labs
 
Building a REST API Microservice for the DevNet API Scavenger Hunt
Building a REST API Microservice for the DevNet API Scavenger HuntBuilding a REST API Microservice for the DevNet API Scavenger Hunt
Building a REST API Microservice for the DevNet API Scavenger HuntAshley Roach
 
How to implement ruby on rails testing practices to build a successful web ap...
How to implement ruby on rails testing practices to build a successful web ap...How to implement ruby on rails testing practices to build a successful web ap...
How to implement ruby on rails testing practices to build a successful web ap...Katy Slemon
 
Ruby on Rails All Hands Meeting
Ruby on Rails All Hands MeetingRuby on Rails All Hands Meeting
Ruby on Rails All Hands MeetingDan Davis
 
React on rails v6.1 at LA Ruby, November 2016
React on rails v6.1 at LA Ruby, November 2016React on rails v6.1 at LA Ruby, November 2016
React on rails v6.1 at LA Ruby, November 2016Justin Gordon
 
Inflectracon2020: Advantages of Integrating a DevSecOps Pipeline with the Spi...
Inflectracon2020: Advantages of Integrating a DevSecOps Pipeline with the Spi...Inflectracon2020: Advantages of Integrating a DevSecOps Pipeline with the Spi...
Inflectracon2020: Advantages of Integrating a DevSecOps Pipeline with the Spi...Inflectra
 
Emulators as an Emerging Best Practice for API providers
Emulators as an Emerging Best Practice for API providersEmulators as an Emerging Best Practice for API providers
Emulators as an Emerging Best Practice for API providersPostman
 
Scaling, Securing, Managing, and Publishing Power Platform Custom Connectors....
Scaling, Securing, Managing, and Publishing Power Platform Custom Connectors....Scaling, Securing, Managing, and Publishing Power Platform Custom Connectors....
Scaling, Securing, Managing, and Publishing Power Platform Custom Connectors....Eric Shupps
 
The details of CI/CD environment for Ruby
The details of CI/CD environment for RubyThe details of CI/CD environment for Ruby
The details of CI/CD environment for RubyHiroshi SHIBATA
 
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...Nilesh Panchal
 

Similar to Basic RSpec 2 (20)

Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
       Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber       Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
 
Capybara and cucumber with DSL using ruby
Capybara and cucumber with DSL using rubyCapybara and cucumber with DSL using ruby
Capybara and cucumber with DSL using ruby
 
Rails automatic test driven development
Rails automatic test driven developmentRails automatic test driven development
Rails automatic test driven development
 
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
 
Rethinking Testing
Rethinking TestingRethinking Testing
Rethinking Testing
 
Robotframework
RobotframeworkRobotframework
Robotframework
 
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
 
Ruby on Rails Penetration Testing
Ruby on Rails Penetration TestingRuby on Rails Penetration Testing
Ruby on Rails Penetration Testing
 
Building a REST API Microservice for the DevNet API Scavenger Hunt
Building a REST API Microservice for the DevNet API Scavenger HuntBuilding a REST API Microservice for the DevNet API Scavenger Hunt
Building a REST API Microservice for the DevNet API Scavenger Hunt
 
How to implement ruby on rails testing practices to build a successful web ap...
How to implement ruby on rails testing practices to build a successful web ap...How to implement ruby on rails testing practices to build a successful web ap...
How to implement ruby on rails testing practices to build a successful web ap...
 
Ruby on Rails All Hands Meeting
Ruby on Rails All Hands MeetingRuby on Rails All Hands Meeting
Ruby on Rails All Hands Meeting
 
React on rails v6.1 at LA Ruby, November 2016
React on rails v6.1 at LA Ruby, November 2016React on rails v6.1 at LA Ruby, November 2016
React on rails v6.1 at LA Ruby, November 2016
 
Inflectracon2020: Advantages of Integrating a DevSecOps Pipeline with the Spi...
Inflectracon2020: Advantages of Integrating a DevSecOps Pipeline with the Spi...Inflectracon2020: Advantages of Integrating a DevSecOps Pipeline with the Spi...
Inflectracon2020: Advantages of Integrating a DevSecOps Pipeline with the Spi...
 
React on rails v4
React on rails v4React on rails v4
React on rails v4
 
Emulators as an Emerging Best Practice for API providers
Emulators as an Emerging Best Practice for API providersEmulators as an Emerging Best Practice for API providers
Emulators as an Emerging Best Practice for API providers
 
Scaling, Securing, Managing, and Publishing Power Platform Custom Connectors....
Scaling, Securing, Managing, and Publishing Power Platform Custom Connectors....Scaling, Securing, Managing, and Publishing Power Platform Custom Connectors....
Scaling, Securing, Managing, and Publishing Power Platform Custom Connectors....
 
Speedy TDD with Rails
Speedy TDD with RailsSpeedy TDD with Rails
Speedy TDD with Rails
 
The details of CI/CD environment for Ruby
The details of CI/CD environment for RubyThe details of CI/CD environment for Ruby
The details of CI/CD environment for Ruby
 
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 

Recently uploaded

Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...liera silvan
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 

Recently uploaded (20)

Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 

Basic RSpec 2

  • 1. The Basic of RSpec 2 Authors: Triet Le – Truc Nguyen
  • 2. Agenda •TDD, BDD intro •RSpec intro •Expectation - Matcher •Mocking-Stubing •RSpec Rails •Controller Specs •Model Specs •Rspec vs Cucumber •Feature Specs •Code coverage tool: SimpleCov, Rcov •Example Code
  • 5. •It's 'tests' that 'drive' the 'development' •Make sure that No code goes into production without associated tests •Benefits of TDD: http://agilepainrelief.com/notesfromatooluser/2 008/10/advantages-of-tdd.html TDD
  • 7. BDD •BDD is based on TDD •BDD is specifying how your application should work, rather than verifying that it works. •Behaviour-Driven Development is about implementing an application by describing its behavior from the perspective of its stakeholders. (Rspec book)
  • 9. RSpec •Rspec is unit-testing framework for Ruby programming language •RSpec is BDD •Rspec's strongly recommended with TDD
  • 10. How
  • 11. describe `Class` do before do # Setup something end it “should return something“ do # actual_result.should matcher(expected_result) end end HOW An Example E x a m p l e g r o u p This is a Spec file
  • 13. Expectation - Matcher •Basic structure of an rspec expectation o Old syntax  actual.should matcher(expected)  actual.should_not matcher(expected) o New  expect(actual).to eq(expected)  expect(actual).not_to eq(expected) •For example o 5.should eq(5) / expect(5).to eq(5) o 5.should_not eq(4) / expect(5).not_to eq(5)
  • 16. Mocking - Stubbing Rspec-mocks is a test-double framework for rspec with support for methods o mock a `fake` object o stubs o message expectations on generated test-doubles and real objects alike.
  • 17. Mocking - Stubbing •Test double o book = double("book") •Method Stubs o book.stub(:title) { "The RSpec Book" } o book.stub(:title => "The RSpec Book") o book.stub(:title).and_return("The RSpec Book") •Message expectations o person = double("person") o Person.should_receive(:find) { person } •should_receive vs stub
  • 18. Mocking - Stubbing •Good for o Speed up testing o Real object is unavailable o Difficult to access from a test environment: External services o Queries with complicated data setup
  • 19. Mocking - Stubbing •Problems o Simulated API gets out of sync with actual API o Leads to testing implementation, not effect o Demands on integration and exploratory testing higher with mocks o Less value per line of test code!
  • 21. Rspec-rails •Add to Gemfile •group :test, :development do gem "rspec-rails", "~> 2.4"end •Recommended oFactory-girl oGuard-spec oSpork o SimpleCov
  • 22. Rspec-rails source: http://www.rubyfocus.biz/ Views Controller Routes Application, Browser UI Application, Server Helpers Model Selenium RSpec Integration/Request, Cucumber, Webrat, Capybara RSpec Views RSpec Helpers RSpec Controller RSpec Routing RSpec Model Test::Unit Test::Unit Functional Test::Unit Integration Application, Browser UI Application, Server
  • 24. Controller specs •Simulate a single HTTP verb in each example o GET o POST o PUT o DELETE o XHR •Accessable variables o controller, request, response o assigns, cookies, flash, and session
  • 25. Controller specs •Check rendering oCorrect template  response.should render_template oRedirect  response.should redirect_to (url or hash) o Status code  response.code.should eq(200) •Verify variable assignments o Instance variables assigned in the controller to be shared with the view o Cookies sent back with the response  cookies['key']  cookies['key']
  • 26.
  • 27. What need to test in model? Model Specs
  • 28. Model specs •Exists attributes •Association •Model’s validations •Class methods •Instance methods
  • 29. Model specs For detail, a model spec should include: •Attributes o model attributes should have •Association o model association should have •The model’s create method -> check the validation work? o passed valid attributes => should be valid. o fail validations => should not be valid. •Class and instance methods perform as expected
  • 30. Model specs - Example code
  • 32. Model specs - Example rspec model code
  • 37. Rspec vs Cucumber •Both testing frameworks. •Both are used for Acceptance Testing •These are business-case driven Integration Tests o simulate the way a user uses the application, o the way the different parts of your application work together can be found in a way that unit testing will not find. same
  • 38. Rspec vs Cucumber •RSpec and Cucumber are the business readability factor difference CU CU M B ER odraw is that the specification (features) from the test code oproduct owners can provide or review without having to dig through code R SPEC odescribe a step with a Describe, Context or It block that contains the business specification oa little easier for developers to work obut a little harder for non-technical folks
  • 40. Integration test with rspec and capybara (and Senelium) Feature Specs •Introduce •Setup env •Example code
  • 41. Feature Specs - Introduce •high-level tests meant to exercise slices of functionality through an application. Drive the application only via its external interface, usually web pages. •Require the capybara gem, version 2.0.0 or later. Refer to the capybara API for more infos on the methods and matchers •Feature, scenario, background, given DSL <=>describe, it, before each, let. Alias methods that allow to read more as customer tests and acceptance tests.
  • 42. Feature Specs - Setup env First, add Capybara to your Gemfile: In spec/spec_helper.rb, add two require calls for Capybara near the top Capybara’s DSL will be available spec/requests and spec/integration directory
  • 43. Feature Specs - Selenium First, add Capybara to your Gemfile: In spec/spec_helper.rb, add two require calls for Capybara near the top •Run Selenium, just set :js => true Firefox should automatically fire up and run with Selenium. •Capybara.default_driver = :selenium (make Capybara to all your tests - don’t recommend) •Using Rack::Test (default driver) and Selenium (JS driver) by setting the :js attribute (faster if use Selenium for tests that actually require JS)
  • 44. Feature Specs - Sample code When writing integration tests, try to model the test around an actor (user of the system) and the action they are performing.
  • 45. Feature Specs - Sample code
  • 49. •New syntax expectation rspec •Feature specs •Rspec vs cucumber •http://www.slideshare.net/NasceniaIT/tdd-bdd-r-spec •Intergartion test •Setup capypara •Devise with rspec References