SlideShare a Scribd company logo
Rspec Best Practices
      Ruby Social Club Milano
           24 February 2011




                   Andrea Reginato
             twitter/andreareginato
I'll not teach RSpec
You have good books and web sites
                  The RSpec Book
            Rails Test Prescriptions




                   twitter/andreareginato
I'll tell you how I use RSpec
      What I've learned reading books
             some articles on the web
              but most important from
         my everyday job experience




                      twitter/andreareginato
Just a little introduction
 RSpec is a library that focus on
    testing the behavior of your
                everyday project




                  twitter/andreareginato
Why people do not use it
 Probably the testing practice is the
      most diffcult forma mentis a
            developer have to learn




                     twitter/andreareginato
It will save our life, maybe
  In the long term, you will not check
        your web page everytime you
     make a new feature, but you will
            check if your test passed




                      twitter/andreareginato
Short descriptions
# wrong
it "should have 422 status code if an
    unexpected params will be added" do
  response.should respond_with 422
end

# correct
context "when not valid"
  it { should respond_with 422 }
(over)use contexts
                          and use with/when as keys

# wrong
it "should have 200 status code if
    logged in" do
  response.should respond_with 200
end

# correct
context "when logged in" do
  it { should respond_with 200 }
end
Describe methods
# wrong
describe "the authenticate method for
           User logged in" ...
describe "if the user is an admin" do


# correct
describe ".authenticate" do
describe "#admin?"
Single expectation test
# wrong
it "should create a resource" do
  response.should respond_with_content_type(:json)
  response.should assign_to(:resource)
end


# correct
it { should respond_with_content_type(:json) }
it { should assign_to(:resource) }
Test the edge cases
# sample action
def destroy
  @resource = Resource.where(:id => params[:id])
  if @resource
    @resource.destroy
    head 204
  else
    render :template => "shared/404", :status => 404,
  end
end



What would you test ?
Test the edge case
# correct
describe "#destroy" do
  context "when resource is found" do
    it "should render_with 204"
    it "should assign @resource"
  context "when resource is not found" do
    it "should render with 404"
    it "should not assign @resource"
  end
end


Is this enough ?
Use the subject
# wrong
it { assigns("message").should match
     /The resource name is Genoveffa/ }
it { assigns("message").should match
     /it was born in Billyville/ }
it { assigns("message").creator.should match
     /Claudiano/ }

# correct
subject { assigns("message") }
it { should match /The resource name is Genoveffa/ }
it { should match /it was born in Billyville/ }
its(:creator) { should match /Claudiano/ }
Mock or not to mock
# wrong (a really personal point of view)
1) It's wrong when you never mock and recreate by code
a “medium complex” situation and it's time consuming
2) It's wrong when you mock everything. You have a light
system and the big advantage of independence, but you loose
part of the control on your application.

# correct
# simulate authenticated user
controller.stub!(:authenticate).and_return(true)
# simulate current user
controller.stub(:current_user).and_return(current_user)
# simulate not found record
Resource.stub(:where).with(created_from: id)
                     .and_return(false)
Create data when needed
# wrong
You shouldn't use fixtures
* loose control on our data
* load everything all the time
* difficult to find problems

# correct
describe "User"
  describe ".top" do
    before { 3.times { Factory(:user) } }
    it { User.top(2).should have(2).item }
  end
end
Shared examples to DRY
# wrong
context "when own resources" do
  it "should have it" do
    resource = Factory("user")
    do_get format: json
    assigns(users).should include(resource)
  end
end

context "when does not own resource" do
  it "should not have it" do
    not_owned_resource = Factory("unknown")
    do_get format: json
    assigns(users).should_not include(not_owned_resource)
  end
end
Shared examples to DRY
# correct (but why?)
shared_examples for "a secure resource" do
  context "when own the resource" do
    it "should have it" do
      resource = Factory("user")
      do_get format: json
      assigns(users).should include(resource)
    end
  end
  context "when does not own resource" do
    it "should not have it" do
      not_owned_resource = Factory("unknown")
      do_get format: json
      assigns(users).should_not include(not_owned_resource)
    end
  end
end
Shared examples to DRY
# correct (apply DRY on tests)
describe "#show" do
  it_should_behave_like "a secure resource"
  it_should_behave_like "a findable resource"
  it_should_behave_like "a secure resource"
  context "when not logged in" do
    it_should_handle "a not authenticated request"
  end
end
More?
# wrong
Do not use RSpec

# correct
Start using RSpec 2 into your next project
Thanks
# more info
Click here to fnd a public document with a
summary of all best practices


# contacts
andrea.reginato@gmail.com
twitter/andreareginato

More Related Content

PDF
Rspec API Documentation
PDF
Automated testing with RSpec
PPTX
Introduction to testing in Rails
PDF
Testing Ruby with Rspec (a beginner's guide)
PPTX
Rspec presentation
PPT
Ruby on Rails testing with Rspec
PDF
RSpec 3: The new, the old, the good
PPTX
RSpec and Rails
Rspec API Documentation
Automated testing with RSpec
Introduction to testing in Rails
Testing Ruby with Rspec (a beginner's guide)
Rspec presentation
Ruby on Rails testing with Rspec
RSpec 3: The new, the old, the good
RSpec and Rails

What's hot (20)

PPTX
RSpec: What, How and Why
PDF
TDD with phpspec2
PDF
Djangocon 2014 angular + django
PDF
Django REST Framework
PPT
Testing Javascript with Jasmine
PDF
RSpec User Stories
PDF
TDD with PhpSpec
PDF
Building an API with Django and Django REST Framework
PPTX
2019-08-23 API contract testing with Dredd
PPT
Testing in AngularJS
PPTX
Zero to Testing in JavaScript
KEY
I18n
PDF
How to write easy-to-test JavaScript
PDF
The Best (and Worst) of Django
PDF
Build REST API clients for AngularJS
PPT
Integration and Acceptance Testing
ODP
Nexthink Library - replacing a ruby on rails application with Scala and Spray
PDF
Scalable web application architecture
PPT
Create a web-app with Cgi Appplication
RSpec: What, How and Why
TDD with phpspec2
Djangocon 2014 angular + django
Django REST Framework
Testing Javascript with Jasmine
RSpec User Stories
TDD with PhpSpec
Building an API with Django and Django REST Framework
2019-08-23 API contract testing with Dredd
Testing in AngularJS
Zero to Testing in JavaScript
I18n
How to write easy-to-test JavaScript
The Best (and Worst) of Django
Build REST API clients for AngularJS
Integration and Acceptance Testing
Nexthink Library - replacing a ruby on rails application with Scala and Spray
Scalable web application architecture
Create a web-app with Cgi Appplication
Ad

Viewers also liked (7)

PDF
An Introduction to Faye
PDF
Most Effective Ways Of Permanently Removing Keloid Scarring
PPT
ドメインロジックの実装方法とドメイン駆動設計
PDF
C#実装から見るDDD(ドメイン駆動設計)
PDF
GOCON Autumn (Story of our own Monitoring Agent in golang)
PDF
Fast and Reliable Swift APIs with gRPC
PPTX
Gocon2017:Goのロギング周りの考察
An Introduction to Faye
Most Effective Ways Of Permanently Removing Keloid Scarring
ドメインロジックの実装方法とドメイン駆動設計
C#実装から見るDDD(ドメイン駆動設計)
GOCON Autumn (Story of our own Monitoring Agent in golang)
Fast and Reliable Swift APIs with gRPC
Gocon2017:Goのロギング周りの考察
Ad

Similar to RSpec 2 Best practices (20)

PDF
Oto Brglez - Tips for better tests
PDF
Cucumber & BDD
PDF
Getting Up and Running with BDD on Rails
PDF
Getting Up and Running with BDD on Rails
KEY
Ruby For Startups
PDF
Continuous (Production) Integration: Ruby on Rails Application Monitoring wit...
PDF
Having Fun with Play
ODP
Rtt preso
PDF
Crossing the Bridge: Connecting Rails and your Front-end Framework
PDF
Rspec and Capybara Intro Tutorial at RailsConf 2013
PDF
Good Coding Practices with JavaScript
PPTX
SharePoint Saturday NYC - SharePoint and jQuery, what I wish I would have kno...
PPTX
#SPSEMEA SharePoint & jQuery - What I wish I would have known a year ago..
PDF
Rails OO views
PDF
Connecting with the enterprise - The how and why of connecting to Enterprise ...
PDF
Behaviour driven infrastructure
PDF
Getting Started with React-Nathan Smith
PDF
Getting Started with React
PPT
Selenium and Cucumber Selenium Conf 2011
PPT
Cucumber Presentation Kiev Meet Up
Oto Brglez - Tips for better tests
Cucumber & BDD
Getting Up and Running with BDD on Rails
Getting Up and Running with BDD on Rails
Ruby For Startups
Continuous (Production) Integration: Ruby on Rails Application Monitoring wit...
Having Fun with Play
Rtt preso
Crossing the Bridge: Connecting Rails and your Front-end Framework
Rspec and Capybara Intro Tutorial at RailsConf 2013
Good Coding Practices with JavaScript
SharePoint Saturday NYC - SharePoint and jQuery, what I wish I would have kno...
#SPSEMEA SharePoint & jQuery - What I wish I would have known a year ago..
Rails OO views
Connecting with the enterprise - The how and why of connecting to Enterprise ...
Behaviour driven infrastructure
Getting Started with React-Nathan Smith
Getting Started with React
Selenium and Cucumber Selenium Conf 2011
Cucumber Presentation Kiev Meet Up

Recently uploaded (20)

PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
A Presentation on Artificial Intelligence
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Encapsulation theory and applications.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Big Data Technologies - Introduction.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Electronic commerce courselecture one. Pdf
PDF
KodekX | Application Modernization Development
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation_ Review paper, used for researhc scholars
“AI and Expert System Decision Support & Business Intelligence Systems”
A Presentation on Artificial Intelligence
Mobile App Security Testing_ A Comprehensive Guide.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Encapsulation theory and applications.pdf
Understanding_Digital_Forensics_Presentation.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Big Data Technologies - Introduction.pptx
cuic standard and advanced reporting.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Electronic commerce courselecture one. Pdf
KodekX | Application Modernization Development
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Per capita expenditure prediction using model stacking based on satellite ima...

RSpec 2 Best practices

  • 1. Rspec Best Practices Ruby Social Club Milano 24 February 2011 Andrea Reginato twitter/andreareginato
  • 2. I'll not teach RSpec You have good books and web sites The RSpec Book Rails Test Prescriptions twitter/andreareginato
  • 3. I'll tell you how I use RSpec What I've learned reading books some articles on the web but most important from my everyday job experience twitter/andreareginato
  • 4. Just a little introduction RSpec is a library that focus on testing the behavior of your everyday project twitter/andreareginato
  • 5. Why people do not use it Probably the testing practice is the most diffcult forma mentis a developer have to learn twitter/andreareginato
  • 6. It will save our life, maybe In the long term, you will not check your web page everytime you make a new feature, but you will check if your test passed twitter/andreareginato
  • 7. Short descriptions # wrong it "should have 422 status code if an unexpected params will be added" do response.should respond_with 422 end # correct context "when not valid" it { should respond_with 422 }
  • 8. (over)use contexts and use with/when as keys # wrong it "should have 200 status code if logged in" do response.should respond_with 200 end # correct context "when logged in" do it { should respond_with 200 } end
  • 9. Describe methods # wrong describe "the authenticate method for User logged in" ... describe "if the user is an admin" do # correct describe ".authenticate" do describe "#admin?"
  • 10. Single expectation test # wrong it "should create a resource" do response.should respond_with_content_type(:json) response.should assign_to(:resource) end # correct it { should respond_with_content_type(:json) } it { should assign_to(:resource) }
  • 11. Test the edge cases # sample action def destroy @resource = Resource.where(:id => params[:id]) if @resource @resource.destroy head 204 else render :template => "shared/404", :status => 404, end end What would you test ?
  • 12. Test the edge case # correct describe "#destroy" do context "when resource is found" do it "should render_with 204" it "should assign @resource" context "when resource is not found" do it "should render with 404" it "should not assign @resource" end end Is this enough ?
  • 13. Use the subject # wrong it { assigns("message").should match /The resource name is Genoveffa/ } it { assigns("message").should match /it was born in Billyville/ } it { assigns("message").creator.should match /Claudiano/ } # correct subject { assigns("message") } it { should match /The resource name is Genoveffa/ } it { should match /it was born in Billyville/ } its(:creator) { should match /Claudiano/ }
  • 14. Mock or not to mock # wrong (a really personal point of view) 1) It's wrong when you never mock and recreate by code a “medium complex” situation and it's time consuming 2) It's wrong when you mock everything. You have a light system and the big advantage of independence, but you loose part of the control on your application. # correct # simulate authenticated user controller.stub!(:authenticate).and_return(true) # simulate current user controller.stub(:current_user).and_return(current_user) # simulate not found record Resource.stub(:where).with(created_from: id) .and_return(false)
  • 15. Create data when needed # wrong You shouldn't use fixtures * loose control on our data * load everything all the time * difficult to find problems # correct describe "User" describe ".top" do before { 3.times { Factory(:user) } } it { User.top(2).should have(2).item } end end
  • 16. Shared examples to DRY # wrong context "when own resources" do it "should have it" do resource = Factory("user") do_get format: json assigns(users).should include(resource) end end context "when does not own resource" do it "should not have it" do not_owned_resource = Factory("unknown") do_get format: json assigns(users).should_not include(not_owned_resource) end end
  • 17. Shared examples to DRY # correct (but why?) shared_examples for "a secure resource" do context "when own the resource" do it "should have it" do resource = Factory("user") do_get format: json assigns(users).should include(resource) end end context "when does not own resource" do it "should not have it" do not_owned_resource = Factory("unknown") do_get format: json assigns(users).should_not include(not_owned_resource) end end end
  • 18. Shared examples to DRY # correct (apply DRY on tests) describe "#show" do it_should_behave_like "a secure resource" it_should_behave_like "a findable resource" it_should_behave_like "a secure resource" context "when not logged in" do it_should_handle "a not authenticated request" end end
  • 19. More? # wrong Do not use RSpec # correct Start using RSpec 2 into your next project
  • 20. Thanks # more info Click here to fnd a public document with a summary of all best practices # contacts andrea.reginato@gmail.com twitter/andreareginato