rspec and rails

27
RSpec and Rails Alan Hecht http://alanhecht.me

Upload: alan-hecht

Post on 15-Jan-2015

5.166 views

Category:

Technology


2 download

DESCRIPTION

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

TRANSCRIPT

Page 1: RSpec and Rails

RSpec and Rails

Alan Hechthttp://alanhecht.me

Page 2: RSpec and Rails

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

Page 3: RSpec and Rails

Creating a Rails Project

• rails new cart_rspec

• Edit the Gemfile

• bundle install• rails generate rspec:install

Page 4: RSpec and Rails

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

Page 5: RSpec and Rails

Set up the project

• Use railswizard to create a template– We’ll be creating the shopping cart

• Create rvmrc file

• bundle install

• Git setup

Page 6: RSpec and Rails

Set up the Product & Cart

• Generate product scaffolding• Generate cart scaffolding• Generate line_item scaffolding

• RSpec tests were created with scaffolding

• Commit changes

Page 7: RSpec and Rails

RSpec - Models

Page 8: RSpec and Rails

Run Tests

• Update database– rake db:migrate

• Run RSpec– rake spec

Page 9: RSpec and Rails

RSpec Options

• In .rspec file– Documentation format• --format doc

– HTML format• --format html:/path/to/file

Page 10: RSpec and Rails

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

Page 11: RSpec and Rails

RSpec - Models

Page 12: RSpec and Rails

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

Page 13: RSpec and Rails

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

Page 14: RSpec and Rails

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”)

Page 15: RSpec and Rails

Controllers - Example

Page 16: RSpec and Rails

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”

Page 17: RSpec and Rails

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

Page 18: RSpec and Rails

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

Page 19: RSpec and Rails

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

Page 20: RSpec and Rails

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

Page 21: RSpec and Rails

RSpec - Views

Page 22: RSpec and Rails

RSpec - Views

• Describe contains view path– Optional but useful– If ‘render’ doesn’t contain arguments, use the

comment on the describe line

Page 23: RSpec and Rails

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

Page 24: RSpec and Rails

RCov - Output

Page 25: RSpec and Rails

RCov - Tips

• No need to try for 100% test coverage

• Good goal would be around 70 – 80%

Page 26: RSpec and Rails

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

Page 27: RSpec and Rails

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