RSpec and Rails
Upcoming SlideShare
Loading in...5
×
 

RSpec and Rails

on

  • 5,888 views

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

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

Given

Statistics

Views

Total Views
5,888
Views on SlideShare
5,888
Embed Views
0

Actions

Likes
3
Downloads
58
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

RSpec and Rails RSpec and Rails Presentation Transcript

  • RSpec and Rails
    Alan Hecht
    http://alanhecht.me
  • 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
  • Creating a Rails Project
    rails new cart_rspec
    Edit the Gemfile
    bundle install
    rails generate rspec:install
  • Railswizard.org
    Railswizard.org allows you to specify options when creating a Rails 3 project
    Rails 3 allows for flexibility in what’s used
    Database/ORM
    ActiveRecord, Mongomapper
    Unit Testing Framework
    TestUnit, Rspec
    JavaScript Framework
    jQuery, Prototype
  • Set up the project
    Use railswizard to create a template
    We’ll be creating the shopping cart
    Create rvmrc file
    bundle install
    Git setup
  • Set up the Product & Cart
    Generate product scaffolding
    Generate cart scaffolding
    Generate line_item scaffolding
    RSpec tests were created with scaffolding
    Commit changes
  • RSpec - Models
  • Run Tests
    Update database
    rake db:migrate
    Run RSpec
    rake spec
  • RSpec Options
    In .rspec file
    Documentation format
    --format doc
    HTML format
    --format html:/path/to/file
  • 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
  • RSpec - Models
  • 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
  • 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
  • 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”)
  • Controllers - Example
  • 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”
  • 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
  • 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
  • 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
  • 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
  • RSpec - Views
  • RSpec - Views
    Describe contains view path
    Optional but useful
    If ‘render’ doesn’t contain arguments, use the comment on the describe line
  • 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
  • RCov - Output
  • RCov - Tips
    No need to try for 100% test coverage
    Good goal would be around 70 – 80%
  • 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
    discover.rb
  • 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
    autotest-growl
    Add to rails root .autotest file