Rails port/Testing

From OpenStreetMap Wiki
Jump to navigation Jump to search

Rails has a full testing framework built in. Hopefully we can have enough tests that when run, will know whether the code has changed in a way that will cause a problem. Most of the test data for tests is loaded from test/fixtures automatically. Some of the test data is embedded in the tests.

Within your rails_port directory, run

 rake test

This will copy the database schema of the development database and use it for the test db, and also run all the tests.

If you want to run only one test file run the command (this example is for the amf controller):

 ruby test/functional/amf_controller_test.rb

When you are trying to debug a specific test within a file append -n and the function name (for example):

 ruby test/functional/amf_controller_test.rb -n test_whichway_toobig

These tests are automatically running every commit with the results shown at http://cruise.shaunmcdonald.me.uk/

To have the tests auto run whenever you save take a look at the ZenTest gem/autotest. It is clever in that it will only run the tests that have code that has had changes in it http://nubyonrails.com/articles/autotest-rails


To get an idea of the coverage of the code install the rcov gem:

 sudo gem install rcov

and then run the command:

 rcov --rails -x /Gems/ test/*/*.rb

Gotchas

  • You need to be careful with some of the fixtures, as yes, no, true, false, etc have a magic meaning in yaml. So you need to quote the strings instead. [1]
  • If you are testing multiple controllers, then you need to use a integration test rather than a functional test. Note that these are not as documented online due to them being relatively new. Please don't introduce any new with_controller sections of functional tests.
  • In integration testing assert_selects will fail after a redirect if you don't manually set @html_document to nil [2]