IVReview :: Test Case Rails generate a framework for unit and functional tests. You can get pretty good test coverage by filling in the framework with tests for the functionality you write. There are two important points to test in a Rails application: • Testing the Models. • Testing the Controllers.Testing Models: When you generate a model with the generate script, Rails also generates a unit test script for the model in the test directory. It also creates a fixture, a yaml file containing test data to be loaded into the testapp_test database.Unit Test: A test of individual programs or modules in order to ensure that there are no analysis or programming errors is known as unit testing. As in other languages, Ruby provides a framework in its standard library for setting up, organizing and running tests called Test::Unit. An unit testing provides three basic functionalities: • A way to define basic pass/fail tests. • A say to gather like tests together and run them as a group • Tools for running single tests or whole groups of tests.Code for Unit Testing: require test_helper class ApplicantTest < ActiveSupport::TestCase testsome_undefined_variable assert true "should not save without email_id" do applicant = Applicant.new assert !applicant.save some_undefined_variable assert true end endIn terminal
Unit Testing Case Summary: Test case id Description Expected Results Actual Results Status1 Enter the email_id It should raise It is not accepting Pass as Blank error2 Enter email_id with It should accept to It is not accept Fail varchar(20) proceed more then 203 Enter email_id with It should accept to It is not accepting Pass special characters proceed4 Enter a email_id It should accept to It is not accepting Fail with numerical proceedTesting Controllers: Controller testing is also known as functional testing. Functional testing tests the following type of functionalities of the controllers: • Is the response redirected as expected ? • Is the expected template rendered? • Is the routing as expected • Does the response contain the expected tags? Functional Test: 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.Code for Functional Testing: require test_helper class MailersControllerTest < ActionController::TestCase setup do @user = users(:one) end test "should get index" do get :index assert_response :success assert_not_nil assigns(:users) end test "should get new" do get :new assert_response :success end
test "should create user" do assert_difference(User.count) do post :create, :user => @user.attributes end assert_redirected_to user_path(assigns(:user)) end test "should show user" do get :show, :id => @user.to_param assert_response :success end test "should get edit" do get :edit, :id => @applicant.to_param assert_response :success end test "should update applicant" do put :update, :id => @user.to_param, :user => @user.attributes assert_redirected_to user_path(assigns(:user)) end test "should destroy user" do assert_difference(User.count, 1) do delete :destroy, :id => @user.to_param end assert_redirected_to users_path end endIn Terminal:
Functional Testing Case Summary: Test case id Description Expected Results Actual Results Status1 Get index It should get index It is accepting/ Pass page/ if is not get display page error 2 Get new It should diplay It is accepting Pass create page3 Get new/submit It should create Its accepting/not Pass/Fail record/not create accepting4 Get show It should display Its accepting Pass created record5 Get destroy I should delete Its accepting Pass selected recordUsing Rake for testing: We can use rake utility to test our applications by Various kind of testing strategy. Here are few important commands. • $rake test Test all unit tests and functional tests (and integration tests, if they exist). • $rake test:functionals Run all functional tests. • $rake test:units Run all unit tests. • $rake test:integration Run all integration tests. • $rake test:plugins Run all test in ./vendor/plugins/**/test. • $rake test:recent Run tests for models and controllers that have been modified in the last 10 minutes:Some Rake Utility: • $rake db:test:clone Recreate the test database from the current environments database schema. • $rake db:test:clone_structure Recreate the test databases from the development structure. • $rake db:test:prepare Prepare the test database and load the schema. • $rake db:test:purge Empty the test database.