7: Testing Rails Code




Learning Rails show

Summary: <p>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).</p> <p>To read a transcript of the lesson, click the Transcript link on the left.</p> <p><strong>The heart of the lesson is the audio</strong>; these notes are supplementary. So please listen to the audio, or read the transcript, before making use of these notes.</p> <h2>Code Examples</h2> <p>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:</p> <pre> def test_podcast_does_not_exist podcast = Podcast.find_by_title('Intro') assert_nil podcast end </pre> <p>Another example checking to see if the test database contains three podcasts loaded from fixtures might read:</p> <pre> def test_three_podcasts_exist assert_equal, Podcast.find(:all), 3 end </pre> <p>Here’s an example of a fixture, illustrating how fixtures can use model associations. An entry in the podcast.yml file might read:</p> <pre> episodeone: name: Introductory Podcast listeners: anne, bob, charlie </pre> <p>and entries in the listeners.yml file would read:</p> <pre> anne: name: Anne podcast: episodeone bob: name: Bob podcast: episodeone charlie: name: Charlie podcast: episodeone </pre> <h2>Links to More Resources</h2> <p>See the following sections of the site for links to resources on testing:</p> <ul> <li><a href="/topic/24261-testing-ruby-on-rails-applications-unit">Testing in general</a></li> <li><a href="/topic/24275-using-test-driven-development-tdd-for-web">Test-driven development</a></li> <li><a href="/topic/24263-unit-tests-for-ruby-on-rails">Unit testing</a></li> <li><a href="/topic/24264-ruby-on-rails-functional-testing-for">Functional testing</a></li> <li><a href="/topic/24265-ruby-on-rails-integration-testing">Integration testing</a></li> <li><a href="/topic/24273-test-fixtures-for-ruby-on-railss">Test fixtures</a></li> <li><a href="/topic/24269-tools-for-testing-ruby-on-rails">Testing Toolsl</a></li> <li><a href="/topic/24262-using-behavior-driven-development-bdd-for-web">Behavior-Driven Development</a></li> </ul>