Testing Rails Code
To listen to the lesson, click the Play button on the left. You can also right-click on the download link to save the mp3 file, or you can subscribe in iTunes (just search for Learning Rails).
To read a transcript of the lesson, click the Transcript link on the left.
The heart of the lesson is the audio; these notes are supplementary. So please listen to the audio, or read the transcript, before making use of these notes.
Code Examples
Here’s an example of using asserts. Suppose you have a test that searches for a non-existent podcast episode; you’d want to assert that the result was nil. The test case code would read:
def test_podcast_does_not_exist podcast = Podcast.find_by_title(‘Intro’) assert_nil podcast end
Another example checking to see if the test database contains three podcasts loaded from fixtures might read:
def test_three_podcasts_exist assert_equal, Podcast.find(:all), 3 end
Here’s an example of a fixture, illustrating how fixtures can use model associations. An entry in the podcast.yml file might read:
episodeone: name: Introductory Podcast listeners: anne, bob, charlie
and entries in the listeners.yml file would read:
anne: name: Anne podcast: episodeone bob: name: Bob podcast: episodeone charlie: name: Charlie podcast: episodeone
Links to More Resources
See the following sections of the site for links to resources on testing:
- Testing in general
- Test-driven development
- Unit testing
- Functional testing
- Integration testing
- Test fixtures
- Testing Toolsl
- Behavior-Driven Development