Your SlideShare is downloading. ×
RSpec and Rails
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

RSpec and Rails


Published on

Presentation on using RSpec with Rails. Assumes basic RSpec knowledge. …

Presentation on using RSpec with Rails. Assumes basic RSpec knowledge.


Published in: Technology

  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. RSpec and Rails
    Alan Hecht
  • 2. Installation
    RSpec 2.5
    Only works with Rails 3
    rspec and rspec-rails
    gem install rspec-rails
    What if you’re using Rails 2.x?
    Need to install RSpec 1.3
  • 3. Creating a Rails Project
    rails new cart_rspec
    Edit the Gemfile
    bundle install
    rails generate rspec:install
  • 4. allows you to specify options when creating a Rails 3 project
    Rails 3 allows for flexibility in what’s used
    ActiveRecord, Mongomapper
    Unit Testing Framework
    TestUnit, Rspec
    JavaScript Framework
    jQuery, Prototype
  • 5. Set up the project
    Use railswizard to create a template
    We’ll be creating the shopping cart
    Create rvmrc file
    bundle install
    Git setup
  • 6. Set up the Product & Cart
    Generate product scaffolding
    Generate cart scaffolding
    Generate line_item scaffolding
    RSpec tests were created with scaffolding
    Commit changes
  • 7. RSpec - Models
  • 8. Run Tests
    Update database
    rake db:migrate
    Run RSpec
    rake spec
  • 9. RSpec Options
    In .rspec file
    Documentation format
    --format doc
    HTML format
    --format html:/path/to/file
  • 10. RSpec – Models
    Most similar to using RSpec without Rails
    Everything we learned from the calculator example applies
    Business logic testing should take place in the models
  • 11. RSpec - Models
  • 12. RSpec – Fixtures
    Fixture files go in spec/fixtures
    Need to explicitly load fixtures
    Can load all the fixtures for tests
    Add ‘config.global_fixtures = true’ to spec_helper.rb
  • 13. RSpec - Controllers
    In Rails Test::Unit, Controllers and Views tested together in functional tests
    For RSpec, each is tested separately
    Outer describe contains the controller class
    Needed to run the tests
    Describe in each test has controller action
    Doesn’t affect the test, only by convention
  • 14. Controller Matchers
    RSpec has matchers specifically for controller testing:
    response.should be_success
    response.should be_redirect
    response.should redirect_to(url)
    response.should render_template(‘app/views/products/index’)
    response.should render_template(:partial => “show_cart”)
  • 15. Controllers - Example
  • 16. Stubs – What are they?
    A fake object that returns a pre-determined value for a method call
    Actual object is not called
    Stubs and mocks often used interchangeably
    Mean two different things
    But people generally refer to “Mock Object Frameworks”
  • 17. Stubs – Why?
    Separation of concerns
    Controller doesn’t depend on model
    Models don’t depend on each other
    Each model can be tested in isolation
    Don’t need the database for testing
    Faster tests
    Could use stubs to simulate a network or web service response
  • 18. Stubs – Implications
    Need to have integration tests
    All of your tests could pass, but the site may not function
    Webrat (used by Cucumber) & Capybara are two such integration tools
  • 19. Mocks
    Just like Stubs, but with an expectation to be (or not to be) called
    Test will fail if the mock is (or isn’t) called
    Used in the same way as stubs
  • 20. RSpec - Views
    Verify the presence of fields on the view
    Avoid relying on the layout of the view
    View could break from a design change
    But good for testing groups of elements with a common purpose (like options fields that expand or hide)
    Use XPath or CSS selectors to locate tags
    Strongly recommend CSS selectors
  • 21. RSpec - Views
  • 22. RSpec - Views
    Describe contains view path
    Optional but useful
    If ‘render’ doesn’t contain arguments, use the comment on the describe line
  • 23. Test Coverage
    rcov gem will show you how much of your code has been tested
    rake spec:rcov
    By default, all tests are run
    Need to modify Rakefile to cover only specific areas like models & views
    Output is in the <project>/coverage directory
    HTML viewable in a browser
    Open coverage/index.html
  • 24. RCov - Output
  • 25. RCov - Tips
    No need to try for 100% test coverage
    Good goal would be around 70 – 80%
  • 26. Automatic Testing
    Autotest monitors changes to files in your project
    Every time a test is saved, the test is re-run
    Everytime app code is saved, corresponding test is run
    gem install autotest & autotest-rails-pure
    Create an autotest directory
  • 27. Autotest – Tips
    Be careful when using on a laptop
    Autotest polls for changes
    You’ll either get burnt by a hot laptop or your battery will die, whichever comes first
    On OS X, you can install autotest-fsevent, the file system change triggers autotest execution
    Growl notifications
    Add to rails root .autotest file