Testing Tutorial

Testing is a concept used in many programming languages and web applications. It helps the user test their application and pin out problems that need to be debugged. In ruby, testing has its own METHOD that runs through the code and gives the user a summary of how many tests and assertions were made. Errors and failures that exist in the code are also included in the summary. Rails includes several types of testing. This document focuses on Unit Tests which are used to test models. Functional Testing Tutorial discusses another type of test, which Rails uses to test controllers.

For example, if we wanted to test out our Grantitude page, we would have to write out a test that checks for errors. Here is a test written for Grantitude:

 
require File.dirname(__FILE__) + '/../test_helper'

class GrantTest < Test::Unit::TestCase
  fixtures :grants

  def test_creation
    grant = Grant.create :title => 'my grant'
    assert_equal 'my grant', grant.title, 'grant title should be set'
  end
  
  def test_search
    grant = Grant.find 2
    grant.tag_list = ['owls']
    grant.save
    assert_equal ['owls'], grant.reload.tag_list, 'tag list should have been saved'
    foundgrants = Grant.search 'owls'
    assert_equal grant, foundgrants.first, 'should be able to search by tag'
  end
  
end

  

The first part of this test is testing whether there is actually a grant.


  def test_creation
    grant = Grant.create :title => 'my grant'
    assert_equal 'my grant', grant.title, 'grant title should be set'
  end

  

If there is no grant in Grantitude, the test will report an error. However, it’ll run smoothly if there is a grant.

The second part of the test checks for a grant at a particular location and gives it a tag ‘owls’. It later makes an assertion on whether the tag is ‘owls.’ If it is then no errors or failures are reported back, but if the tag doesn’t return an ‘owls’ then it returns a failure.


  def test_search
    grant = Grant.find 2
    grant.tag_list = ['owls']
    grant.save
    assert_equal ['owls'], grant.reload.tag_list, 'tag list should have been saved'
  end

  

Now, lets go over the basics on what needs to go into a testing script. First every test script needs a particular line that gives your ruby code the ability to write tests.


require ‘test/unit’

  

This is the standard line used, but in the example above we used ‘required’ but we extracted the file from a different source. The next important line of code is the class declaration, where we begin our test script. We’ll give our class a name ‘GrantTest’


class GrantTest < Test::Unit::TestCase

  

There ‘TestCase’ will provide a place your tests. This means that GrantTest will be a subclass of TestCase.

The next important thing that goes into your test are your methods. It is important to note that all your tests must begin with the word test. Some examples are: test_me, testme, test_you.

Here is an example of a test method:


     def test_what

       assert true

     end

The basic test looks like this:


    # simple_test.rb

    require 'test/unit'

    class GrantTest < Test::Unit::TestCase

      def test_what

        assert true
   
      end

    end

One of the key parts of ruby tests is its assertions. Assertions evaluate expressions for expected results and returns either a failure, an error, or a success. Every test needs them, here are some assertions:

assert ( boolean, msg ) assert_equal ( obj1, obj2, msg ) assert_same ( obj1, obj2, msg ) assert_not_same ( obj1, obj2, msg ) assert_nil ( obj, msg ) assert_not_nil ( obj, msg )

Let’s go over the first two assertions.

- ‘assert’ just asserts a ‘true’ or a ‘false,’ its a boring test, because it just makes sure if the expression is true or not. - ‘assert_equal’ compares two objects and determines if its true.

That concludes the simple tutorial of ruby tests. Hopefully, you got a good firsthand look at tutorials. For more information please check out: manuals.rubyonrails.com/read/chapter/20