Personal Software
Engineering - SE350
Rails Testing
Activity
In this activity we will explore the testing framework built into Rails. We will be concentrating on writing and running unit and functional tests. Unit tests should look very familiar as it is the same xUnit test framework you have seen in Java, C and Ruby. Unit tests are run against the model and functional tests are run against the controllers. Before starting the exercise have a quick look at these chapters from “A Guide to Testing the Rails”:
Exercises: (Use Rails
Song app for the following activities)
Edit the test/fixtures/songs.yml fixture to create some song records:
Song1:
id: 1
title: The Long and
year: 1968
<
note: there needs to be a blank line here between
records>
Song2:
id: 2
title: Get Back
year: 1968
Create unit tests in song_test.rb to verify the
title of a song and year is being loaded correctly from the database:
def test_create_title
@song = Song.find(1) # load Song
record with an id=1
assert_equal "The Long and
end
The unit test could also be written like this
inside of song_test.rb –
test “create_title” do
@song = songs(:Song1) # loads song with fixture name of Song1
assert_equal "The Long and
end
Run the tests as described in the Rails Testing Overview:
$ rake db:test:clone_structure
(note: this only needs to be done once unless the database schema
changes)
$ rake test:units (run all unit tests)
Create additional unit tests to verify song year and author name is being
created correctly
What to turn in by
the end of class: