CSC/ECE 517 Fall 2011/ch4 4e cl: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 90: Line 90:
   end
   end
end
end
{| class="wikitable" border="1"
|-
|
! Helper
! Purpose
|-
| https?
| Returns true if the session is mimicking a secure HTTPS request.
|-
|}





Revision as of 00:44, 21 October 2011

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.

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.

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.

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

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. 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

The Four Hashes of the Apocalypse

1.assigns 2.cookies 3.flash 4.session

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. 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.




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 DELETErequest 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

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

Integration Test Helpers

Setup and Teardown

Testing Routes

Conclusion

References


<references/>