CSC/ECE 517 Fall 2012/ch1b 1w39 sn

From Expertiza_Wiki
Revision as of 06:42, 1 October 2012 by Npatowa (talk | contribs)
Jump to navigation Jump to search

Lecture 10 - Testing in Rails

Setup test environment in Rails

Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec<ref>http://rspec.info/</ref>,Cucumber(for behavior driven development),Shoulda <ref>https://github.com/thoughtbot/shoulda#readme</ref>.When we create the rails scaffold for a particular model then it creates the directories unit,functional,integration which contains the different tests for the respective models.After the test cases have been written we need to prepare the test db.

 rake db:migrate
 rake db:test:load

This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in <ref>http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing</ref> After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder->Select Run->All tests in unit.The figure provided below presents a better picture.

If you are using command line then you can use the following options

ruby -Itest test/unit/post_test.rb
Loaded suite unit/post_test
Started
.
Finished in 0.023513 seconds.
 
2 tests, 2 assertions, 0 failures, 0 errors

Unit Testing:

If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this

require 'test_helper'
 
class PostTest < ActiveSupport::TestCase
  # Replace this with your real tests.
  test "the truth" do
    assert true
  end
end

Now if we wanted to add real tests to it then let us take two scenarios 1.Post with empty entries. 2.Post with actual entries The code for these two test cases would look something like this

require 'test_helper'

class PostTest < ActiveSupport::TestCase

  test "Post new empty" do
    p = Post.new
    assert !p.save, "Saved post without title, content, user, or category"
    assert p.invalid?
  end

  test "Post new correct" do
    p = Post.new
    #Post has following fields title,email,content
    p.title = 'General title'
    p.content = 'A new content'
    p.email = 'Azrael@ncsu.edu'
    #place an assert .so as to find out whether this statement is valid or not
    assert p.valid?
  end

end

test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining a test case.The test cases must begin with the name "test". The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object.

Functional Testing

If unit tests covered models then functional tests took care of the controllers.The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc.Lets say we have a application where users are allowed to post and then comment on those posts.After the user has made a comment then he has to get redirected to that particular post page.Here is how the create method of the comment controller looks like

def create
    #@comment = Comment.new(params[:comment])
    if(session[:email] == nil)
      redirect_to :root
      return
    end
    @comment = Comment.new
    @comment.post_id = params[:id]
    @comment.content = params[:content_new]
    @comment.email = session[:email]
    @comment.vote_count = 0

    @post = Post.find(@comment.post_id)

    dateTime = Time.new
    timestamp = dateTime.to_time
    @post.update_attributes(:updated_at => timestamp)

    respond_to do |format|
      if @comment.save
        format.html { redirect_to :back }
        format.json { render json: @comment, status: :created, location: @comment }
      else
        format.html { render action: "new" }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

As it can be seen if no there is no session then no one can comment.If a user is successfully able to comment then he is redirected to the specific post page for which the comment was made.The functional test for this piece of code would look like this

class CommentsControllerTest < ActionController::TestCase
  setup do
    @comment_new = Comment.new(:content => "Comment to create", :email => "test@gm.com", :post_id => 1)
    @post = Post.find(@comment_new.post_id)
    @comment = comments(:one)#The fixtures contain a row named one
  end

test "should create comment" do
    assert_difference('Comment.count') do
      post :create,  { content: @comment_new.content, email: @comment_new.email, post_id: @comment_new.post_id } #parameters that goes with the post request
    end

    assert_redirected_to post_path(assigns(:post))
  end
end

As we see from the code that it is important to set the session variable and also we need to know before hand for which post are we commenting so we set those variables in the setup method itself.Inside the test method we attempt to create a new comment and after that we check in the assert statement whether it has been redirected to the correct path which in this case is the post page for which the comment has been made.

Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The assert_select assertion allows you to do this by using a simple yet powerful syntax.