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
Comments on This Lesson
From: Vitaliy Khudenko Date: 09/10/08 08:08 AM
Subject: comments count issue
I’ve noticed when I post a comment to a lesson the comments count remains unchanged unless the page is manually reloaded.
From: Vitaliy Khudenko Date: 09/10/08 08:08 AM
Subject: typo
The example checking to see if the test database contains three podcasts loaded from fixtures is missing size method and actually should be as:
def test_three_podcasts_exist assert_equal, Podcast.find(:all).size, 3 end
From: ChanHan Hy Date: 08/18/08 04:04 AM
Subject: Good sites resource on testing
It’s important. Great Resources.
From: Juarez P. A. Filho Date: 08/09/08 10:10 AM
Subject: Test is very important
I always search for great resources and tutorials for my collection and I rarely find something about tests. Thanks guys… You’re rock
From: Gurvinder Date: 07/16/08 09:21 PM
Subject: Testing Issues
It’s a very nice topic discussed in this podcast. While going throug your podcast and i almost found all the files and their conceptual reasons too, the only filei was not able to find was the test.rb which you said is under environments. But i did not find any directory named environments under the rootfolder of my app. insted i found the environments folderunder Config Directory. Is it supposed to be there or i have screwed my folder structure.




From: cherrian chin harada Date: 10/08/08 03:03 AM
Subject: Testing?
Since I am total beginner on the subject of RoR, this question might sound stupid. Anywhere, goes… Do you immediately start testing after installing RoR? Will RoR affect any of my normal fuctions such as my daily e-mail, word processing functions,etc…?