Lesson 7
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:

Reader Comments
12 comments
Pls Update ur Web Site
From: Rohit middha, 04/26/10 10:12 PM
Hello, www.buildingwebapps.com,Myself Rohit middha from india. i have persuaing my b.tech in IT field. Please check your website under this page http://www.buildingwebapps.com/podcasts/79332-testing-rails-code/24793-show-notes and the click on links,, the links are not redirected to a specified result.. pls check this and solve the problems.. Thank you.
Pls Update ur Web Site
From: Rohit middha, 04/26/10 10:11 PM
Hello, www.buildingwebapps.com,Myself Rohit middha from india. i have persuaing my b.tech in IT field. Please check your website under this page http://www.buildingwebapps.com/podcasts/79332-testing-rails-code/24793-show-notes and the click on links,, the links are not redirected to a specified result.. pls check this and solve the problems.. Thank you.
Pls Update ur Web Site
From: Rohit middha, 04/26/10 10:11 PM
Hello, www.buildingwebapps.com,Myself Rohit middha from india. i have persuaing my b.tech in IT field. Please check your website under this page http://www.buildingwebapps.com/podcasts/79332-testing-rails-code/24793-show-notes and the click on links,, the links are not redirected to a specified result.. pls check this and solve the problems.. Thank you.
Pls Update ur Web Site
From: Rohit middha, 04/26/10 10:11 PM
Hello, www.buildingwebapps.com,Myself Rohit middha from india. i have persuaing my b.tech in IT field. Please check your website under this page http://www.buildingwebapps.com/podcasts/79332-testing-rails-code/24793-show-notes and the click on links,, the links are not redirected to a specified result.. pls check this and solve the problems.. Thank you.
Pls Update ur Web Site
From: Rohit middha, 04/26/10 10:11 PM
Hello, www.buildingwebapps.com,Myself Rohit middha from india. i have persuaing my b.tech in IT field. Please check your website under this page http://www.buildingwebapps.com/podcasts/79332-testing-rails-code/24793-show-notes and the click on links,, the links are not redirected to a specified result.. pls check this and solve the problems.. Thank you.
Pls Update ur Web Site
From: Rohit middha, 04/26/10 10:11 PM
Hello, www.buildingwebapps.com,Myself Rohit middha from india. i have persuaing my b.tech in IT field. Please check your website under this page http://www.buildingwebapps.com/podcasts/79332-testing-rails-code/24793-show-notes and the click on links,, the links are not redirected to a specified result.. pls check this and solve the problems.. Thank you.
Testing?
From: cherrian chin harada, 10/07/08 08:57 PM
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...?
comments count issue
From: Vitaliy Khudenko, 09/10/08 01:07 AM
I've noticed when I post a comment to a lesson the comments count remains unchanged unless the page is manually reloaded.
typo
From: Vitaliy Khudenko, 09/10/08 01:01 AM
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
Good sites resource on testing
From: ChanHan Hy, 08/17/08 09:43 PM
It's important. Great Resources.
Test is very important
From: Juarez P. A. Filho, 08/09/08 03:58 AM
I always search for great resources and tutorials for my collection and I rarely find something about tests. Thanks guys... You're rock
Testing Issues
From: Gurvinder, 07/16/08 02:12 PM
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.