SlideShare a Scribd company logo
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']
Basic RSpec 2
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

PDF
Testing with Rspec 3
PPTX
RSpec and Rails
PDF
Capybara with Rspec
PPTX
Capybara + RSpec - ruby dsl-based web ui qa automation
PPT
Integration and Acceptance Testing
PDF
FITC - Here Be Dragons: Advanced JavaScript Debugging
PPT
Testing applications using Apache Camel
PPTX
Unit testing JavaScript: Jasmine & karma intro
Testing with Rspec 3
RSpec and Rails
Capybara with Rspec
Capybara + RSpec - ruby dsl-based web ui qa automation
Integration and Acceptance Testing
FITC - Here Be Dragons: Advanced JavaScript Debugging
Testing applications using Apache Camel
Unit testing JavaScript: Jasmine & karma intro

What's hot (20)

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

Viewers also liked (10)

PDF
Automated testing with RSpec
PDF
Testing Ruby with Rspec (a beginner's guide)
PPTX
RSpec: What, How and Why
PDF
MacRuby
KEY
Railsで春から始めるtdd生活
PPT
Ruby on Rails testing with Rspec
PPTX
Rspec 101
PDF
BDD style Unit Testing
PPT
Ruby On Rails: Web-разработка по-другому!
PDF
RSpec 2 Best practices
Automated testing with RSpec
Testing Ruby with Rspec (a beginner's guide)
RSpec: What, How and Why
MacRuby
Railsで春から始めるtdd生活
Ruby on Rails testing with Rspec
Rspec 101
BDD style Unit Testing
Ruby On Rails: Web-разработка по-другому!
RSpec 2 Best practices
Ad

Similar to Basic RSpec 2 (20)

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

Recently uploaded (20)

PDF
VCE English Exam - Section C Student Revision Booklet
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Pharma ospi slides which help in ospi learning
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
VCE English Exam - Section C Student Revision Booklet
O5-L3 Freight Transport Ops (International) V1.pdf
Yogi Goddess Pres Conference Studio Updates
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Pharma ospi slides which help in ospi learning
2.FourierTransform-ShortQuestionswithAnswers.pdf
Final Presentation General Medicine 03-08-2024.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Complications of Minimal Access Surgery at WLH
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Final Presentation General Medicine 03-08-2024.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
GDM (1) (1).pptx small presentation for students
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Pharmacology of Heart Failure /Pharmacotherapy of CHF
human mycosis Human fungal infections are called human mycosis..pptx
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...

Basic RSpec 2