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)

 

  1. Seeding the database – The Rails projects uses the rake task db:seed to populate the database with initial data. Create a seed.rb script in your application’s db directory. Use the project’s seed file as an example.

 

  1. Unit Tests: So far we have been working in the ‘development.sqlite3’ database. Tests run in Rails using the ‘test.sqlite3’ database. Create the test database schema by running the rake task db:test:clone_structure . This will create a duplicate of the current development database structure only, no data. You can verify the rake task executed by looking in your db directory to see if a test.sqlite3 file was created. You can even look at it using the sqlite3 manager.

 

Edit the test/fixtures/songs.yml fixture to create some song records:

Song1:

  id: 1

  title: The Long and Winding Road

  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 Winding Road", @song.title

      end

 

      The unit test could also be written like this inside of song_test.rb

 

            testcreate_title” do

            @song = songs(:Song1)         # loads song with fixture name of Song1

assert_equal "The Long and Winding Road", @song.title

      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:

 

  1. Per group: ONE test folder (zipped) from the Song application which includes the tests created during this activity. Note the last names of all group members present in the Dropbox comment section.