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. 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
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
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
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
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
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
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 discover.rb
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 autotest-growl Add to rails root .autotest file