CSC/ECE 517 Fall 2011/ch4 4e cl

From Expertiza_Wiki
Jump to navigation Jump to search

CSC/ECE 517 Fall 2010/ch1 4e cl


Introduction

In this Wiki, we introduce the testing in Ruby Rails. We give brief introduction of different kinds of tests and give the procedure about each kind.

What is Software Test

Software testing is an investigation conducted to provide stakeholders with information about the quality of the product or service under test.[1] Software testing can also provide an objective, independent view of the software to allow the business to appreciate and understand the risks of software implementation. Test techniques include, but are not limited to, the process of executing a program or application with the intent of finding software bugs (errors or other defects). Software testing can be stated as the process of validating and verifying that a software program/application/product: 1.Meets the requirements that guided its design and development; 2.Works as expected; and 3.Can be implemented with the same characteristics.

Software testing, depending on the testing method employed, can be implemented at any time in the development process. However, most of the test effort occurs after the requirements have been defined and the coding process has been completed. As such, the methodology of the test is governed by the software development methodology adopted.<ref>http://en.wikipedia.org/wiki/Software_testing</ref>

What is Ruby on Rails

Ruby on Rails, also called Rails or RoR, is an open source web application framework for the Ruby programming language. It uses the Model-View-Controller(MVC) architecture pattern to organize the application programming. Ruby on Rails is separated into various packages, namely ActiveRecord, ActiveResource, ActionPack, ActiveSupport and ActionMailer. It is often installed using RubyGem, which is a package manager including with current versions of Ruby.<ref>http://rubyonrails.org/</ref>

The MVC Architecture

Model-View-Controller(MVC) is a software architecture, currently considered anarchitectural pattern used in software engineering. The model manages the behaviour and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller). In event-driven systems, the model notifies observers (usually views) when the information changes so that they can react. The view renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for different purposes. A viewport typically has a one to one correspondence with a display surface and knows how to render to it. The controller receives user input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and a viewport to perform actions based on that input. <ref>http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller</ref>

Why need Software Test for Rails Application

Ruby on Rails, often shortened to Rails or RoR, is an open source web application framework for the Ruby programming language. Rails makes it super easy to write your tests. It starts by producing skeleton test code in the background while you are creating your models and controllers. By simply running your Rails tests you can ensure your code adheres to the desired functionality even after some major code refactoring. Rails tests can also simulate browser requests and thus you can test your application’s response without having to test it through your browser. <ref>http://guides.rubyonrails.org/testing.html</ref>

Basic Introduction

The Three Environments is Production, Development and Test.

To Set Up the Environment, we may follow the next: There is a folder named "test" which is generate when create the Rail projects. To list contents of this folder $ ls -F test/ We can see: fixtures/ functional/ integration/ test_helper.rb unit/

The Low-Down on Fixtures: Test fixture refers to the fixed state used as a baseline for running tests in software testing. The purpose of a test fixture is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable.<ref>http://en.wikipedia.org/wiki/Test_fixture</ref>For good tests, you’ll need to give some thought to setting up test data. In Rails, you can handle this by defining and customizing fixtures.

Unit Testing

In Rails, unit tests aims to test the models of the application. We also called it as model tests. The code you write up for model tests are placed in the directory of test/unit. What the unit testing in Rails contains is listed as follow: • Load data from fixtures before each unit test • Multiple block syntax for setup and teardown. • The test_helper.rb file, which is a important file in your application and includes the default configuration for all tests. You can put common setup and assertion methods for all tests here. It injects some additional methods into ActiveSupport::TestCase. • A series of assertion methods. Each assertion performs the examination to make sure that things are going as expected. We will show the list of assertions later.

What is A Good Unit Testing?

Our goal for unit testing is to cover almost 100% of the code in models. We recommend writing test file for each individual model. To ensure the difference between a valid and invalid object is as expected, you need to write test to cover validations. The test-driven development advocates that any new logic should be driven by a failing test. Conversely, any new test should fail and trigger a new logic code. If you want your tests to be fewer for each method, please make sure to keep your method simpler and smaller. The following is some tips for writing a good unit testing:

• Keep each of your tests to be small. • Write at least one test for some known errors. For example, without passing arguments. • Write one test for the normal case. • For the case with different branches, write one test for each branch.

There is a tradeoff about the alignment of assertions. If you put all the assertions in single one test method, your test looks more cohesive but it does not allow the tests to run independently. If you put all the assertions in different tests, you can run the test independently but you can not observe the relationships among the tests.

Preparation for A Unit Testing

The scaffolding in Rails will create the model, controller, view and migration. The corresponding full test suite is created at the same time. For example, when you use rails to generate scaffold of “user”, the default test stub for UserTest is created in test/unit/user_post.rb file:

require "test_helper"

class UserTest < ActiveSupport::TestCase

 test "the truth" do
     assert true
   end
end

require 'test_helper' The statement is included in all test files. It means that all the methods in this file will be available to your tests. It declare the default configuration to run the tests. class UserTest < ActiveSupport::TestCase: The UserTest class defines a test case because it inherits from ActiveSupport::TestCase.UserTest thus has all the methods available from ActiveSupport::TestCase. The naming convention for test method is starting with test.

test "the truth" do
     assert true
   end

The method name will be generated by replacing spaces with underscores when you run the test. So the above test method can also be written in the form of :

def test_the_truth
  assert true
end

Before running your test, you need to use the following rake commands to make sure that the test database structure is current.

rake db:migrate runs any pending migrations on the development environment and updates db/schema.rb.
rake db:test:load recreates the test database from the currentdb/schema.rb
rake db:test:clone Recreate the test database from the current environment’s database schema
rake db:test:clone_structure Recreate the test database from the development structure
rake db:test:load: Recreate the test database from the current schema.rb
rake db:test:prepare Check for pending migrations and load the test schema
rake db:test:purge rake db:test:purge

Assertion Methods

The following shows a complete list assertion methods for basic unit test[1]. The left side shows the assertion method names and their corresponding parameters while the right side displays the purpose of each assertion method. The parameter of [msg] is an optional string message where you could place the detailed test failure messages if you want.

Assertion Objective
assert_send( array, [msg] ) Ensures that executing the method listed in array[1] on the object in array[0] with the parameters of array[2 and up] is true.
assert_nil( obj, [msg] ) Ensures that obj.nil? is true.
assert_not_nil( obj, [msg] ) Ensures that obj.nil? is false.
assert_match( regexp, string, [msg] ) Ensures that a string matches the regular expression.
assert_no_match( regexp, string, [msg] ) Ensures that a string doesn’t match the regular expression.
assert( boolean, [msg] ) Ensures that the object/expression is true.
assert_equal( A, B, [msg] ) Ensures that A == B is true. A/B is a object or an expression.
assert_not_equal( A, B, [msg] ) Ensures that A == B is false. A/B is a object or an expression.
assert_same( A, B, [msg] ) Ensures that A.equal?(B) is true. A/B is a object or an expression.
assert_not_same( A, B, [msg] ) Ensures that A.equal?(B) is false. A/B is a object or an expression.
assert_in_delta( expecting, actual, delta, [msg] ) Ensures that the numbers expecting and actual are within delta of each other.
assert_instance_of( class, obj, [msg] ) Ensures that obj is of the class type.
assert_kind_of( class, obj, [msg] ) Ensures that obj is or descends from class.
assert_respond_to( obj, symbol, [msg] ) Ensures that obj has a method called symbol.
assert_operator( obj1, operator, obj2, [msg] ) Ensures that obj1.operator(obj2) is true.
assert_operator( obj1, operator, obj2, [msg] ) Ensures that obj1.operator(obj2) is true.
assert_throws( str_msg, [msg] ) { block } Ensures that the given block throws the str_msg.
assert_raise( exception_1, exception_2, exception_n) { block } Ensures that the given block raises one of the given exceptions.
assert_nothing_raised exception_1, exception_2, exception_n) { block } Ensures that the given block doesn’t raise one of the given exceptions.
flunk( [msg] ) Ensures failure. This is useful to explicitly mark a test that isn’t finished yet.

Below are some custom assertions that Rails adds to the basic unit test. <ref>http://guides.rubyonrails.org/testing.html</ref>

Assertion Objective
assert_difference(expressions, difference = 1, message = nil) {...} Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.
assert_no_difference(expressions, message = nil, &block) Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.
assert(record.valid?) Ensures that the passed record is valid by Active Record standards and returns any error messages if it is not.
assert_recognizes(expected_options, path, extras={}, message=nil) Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options.
assert_generates(expected_path, options, defaults={}, extras = {}, message=nil) Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures.
assert_response(type, message = nil) Asserts that the response comes with a specific status code. You can specify :success to indicate 200, :redirect to indicate 300-399, :missing to indicate 404, or :error to match the 500-599 range
assert_redirected_to(options = {}, message=nil) Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such thatassert_redirected_to(:controller => "weblog") will also match the redirection ofredirect_to(:controller => "weblog", :action => "show") and so on.
assert_template(expected = nil, message=nil) Asserts that the request was rendered with the appropriate template file.

Example of Unit Testing

We can write our unit testing cases now. We will use the live question website as the example. 1 Ensure the username and password are not empty. Either one is missing, the test case will throw the error message.

test "should not be saved without username and password" do
    user = User.new
    assert !user.save
    assert user.new
  end

2) Ensure the post title and content are not empty. Either one is missing, the test case will throw the error message.

test "post attributes must not be empty" do
    post = Post.new
    assert post.invalid?
    assert post.errors[:title].any?
    assert post.errors[:content].any?
  end

3) The following test case is used to test any method that extracts a set of records from the database. First, in line 3-4, it extracts the model class being tested and calls the find method on that model class, resulting in a set of instances of that model. Then in lines 5–8, each instance in the list of matching objects is tested against the block and must return true for the test to pass. Just as importantly, lines 11–14 run the block against all the instances that weren’t returned by the method and assert that the block is false for each one.

def self.should_match_find_method(named_scope, *args, &block)
should "match a find method #{named_scope}" do
 ar_class = self.class.model_class
 found_objects = ar_class.send(named_scope, *args)
 assert !found_objects.blank?
 found_objects.each do |obj|
 assert block.call(obj)
 end

 unfound_objects = ar_class.all - found_objects
 assert !unfound_objects.blank?
 unfound_objects.each do |obj|
 assert !block.call(obj)
 end
 end
 end

<ref>http://en.wikipedia.org/wiki/Unit_testing</ref>

Functional Testing

Functional testing is a type of black box testing that bases its test cases on the specifications of the software component under test. Functions are tested by feeding them input and examining the output, and internal program structure is rarely considered. <ref>http://en.wikipedia.org/wiki/Functional_testing</ref> In Rails, testing the various actions of a single controller is called writing functional tests for that controller. Controllers handle the incoming web requests to your application and eventually respond with a rendered view.

What to Include in Functional Tests

1.was the web request successful? 2.was the user redirected to the right page? 3.was the user successfully authenticated? 4.was the correct object stored in the response template? 5.was the appropriate message displayed to the user in the view? The get method kicks off the web request and populates the results into the response. It accepts 4 arguments: • The action of the controller you are requesting. This can be in the form of a string or a symbol. • An optional hash of request parameters to pass into the action (eg. query string parameters or post variables). • An optional hash of session variables to pass along with the request. • An optional hash of flash values.

Available Request Types for Functional Tests

1.get 2.post 3.put 4.head 5.delete

Integration Testing

Integration tests are used to test the interaction among any number of controllers. They are generally used to test important work flows within your application.<ref>http://en.wikipedia.org/wiki/Integration_testing</ref> Unlike Unit and Functional tests, integration tests have to be explicitly created under the ‘test/integration’ folder within your application. Rails provides a generator to create an integration test skeleton for you. $ rails generate integration_test user_flows

     exists  test/integration/
     create  test/integration/user_flows_test.rb

Here’s what a freshly-generated integration test looks like:

require 'test_helper'
class UserFlowsTest < ActionDispatch::IntegrationTest
  fixtures :all
  # Replace this with your real tests.
  test "the truth" do
    assert true
  end
end


Helper Purpose
https? Returns true if the session is mimicking a secure HTTPS request.
https! Allows you to mimic a secure HTTPS request.
host! Allows you to set the host name to use in the next request.
redirect? Returns true if the last request was a redirect.
follow_redirect! Follows a single redirect response.
request_via_redirect(http_method, path, [parameters], [headers]) Allows you to make an HTTP request and follow any subsequent redirects.
post_via_redirect(path, [parameters], [headers]) Allows you to make an HTTP POST request and follow any subsequent redirects.
get_via_redirect(path, [parameters], [headers]) Allows you to make an HTTP GET request and follow any subsequent redirects.
put_via_redirect(path, [parameters], [headers]) Allows you to make an HTTP PUT request and follow any subsequent redirects.
delete_via_redirect(path, [parameters], [headers]) Allows you to make an HTTP DELETE request and follow any subsequent redirects.
open_session Opens a new session instance.

Example:

require 'test_helper'
class UserFlowsTest < ActionDispatch::IntegrationTest
  fixtures :users
 
  test "login and browse site" do
    # login via https
    https!
    get "/login"
    assert_response :success
 
    post_via_redirect "/login", :username => users(:avs).username, :password => users(:avs).password
    assert_equal '/welcome', path
    assert_equal 'Welcome avs!', flash[:notice]
 
    https!(false)
    get "/posts/all"
    assert_response :success
    assert assigns(:products)
  end
end

<ref>http://weblog.rubyonrails.org/2006/3/1/new-for-rails-1-1-integration-tests</ref>

Running Your Tests

You don’t need to set up and run your tests by hand on a test-by-test basis. Rails comes with a number of rake tasks to help in testing. The table below lists all rake tasks that come along in the default Rakefile when you initiate a Rails project.

Tasks Description
rake test Runs all unit, functional and integration tests. You can also simply runrake as the test target is the default.
rake test:benchmark Benchmark the performance tests
rake test:functionals Runs all the functional tests from test/functional
rake test:integration Runs all the integration tests from test/integration
rake test:plugins Run all the plugin tests from vendor/plugins/*/**/test (or specify with PLUGIN=_name_)
rake test:profile Profile the performance tests
rake test:recent Tests recent changes
rake test:uncommitted Runs all the tests which are uncommitted. Supports Subversion and Git
rake test:units Runs all the unit tests from test/unit

Setup and Teardown

Testing Routes

To set a testing route, we can write the relative codes in the controller.rb file like following:

test "should route to post" do
  assert_routing '/posts/1', { :controller => "posts", :action => "show", :id => "1" }
end

Conclusion

As we can see above, to build and run a test for rails application is fairly easy because it always provide some existing framework to do that. Also we should know that it is very important of testing the rails application. There are also some other approaches and aids for testing such as NullDB, Factory Girl, Machinist, Shoulda, and RSpec, etc.

References


<references/>