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

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
Dr Nic Williams
 

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 (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

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

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 

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