Testing is the process to make sure that everything is developed as required.
Rails generate a framework for unit and functional tests, when user runs the helper script script/generate to create controllers and models. User gets automatic test coverage by adding in the framework with tests for the functionality written.
In Rails application there are two imported things to be tested, following
Before starts with testing, let's create testapp to understand the concept.
Let’s create testing database and set up config/database.yml file correctly.
Create both development and testing databases as shown below
Configure required config/database.yml as shown below
Migration can set up table for, books including their titles, price and description. The following migration sets up this table
Modify the testapp/db/migrate/20080616170315_books.rb file as following below
Above command will create books table in testapp_development database.
Once done creating above database, set up test database using rake command as following below
The given command makes a replica of testapp_development database into testapp_test database. Which means whatever you have in development database, will have it in test database as well.
Rails also generates a unit test script for the model in the test directory, when user generates a model with the generate script, which creates a fixture, a YAMLfile containing test data to be loaded into the testapp_test database. This is the data against which unit tests will run
Let's create two test book records using YAML in test/fixtures/books.yml to keep corresponding tests in these files.
Now replace the existing code in book unit test file test/unit/book_test.rb with the below code
Once done, run the test method by applying below
Here's the output for the test case ran successfully
This is how we can test our Rails Models.
Functional testing will be done to test Controller, which concentrates on functional aspects of the Controller. Following are the functionalities of the controllers, which will be tested under functional testing
Rails framework supports 5 types of requests to write a functional test, user needs to create any of the five HTTP request types, which user’s controller will process
Request type "get" and "post" are the most commonly used in controller testing.
These methods take after four actions of controller, as shown below
All these five methods work the same way. Let’s discuss one method here.
Here, we see how to use get method to test controller.
When you generate a controller with generate, Rails creates a functional test script for the controller as follows
Let's define controller functions list, show, and searchinside app/controllers/book_controller.rb(See below), which contains corresponding tests files.
Please note, show and list method requires views templates. User can define those views and test them.
Here, lets continue without defining those views.
Now let's reuse our test fixture which is in test/fixtures/books.yml file as follows
Add the following test_search_book and test_search_not_found methods totest/functional/book_controller_test.rb to test the functionality of the Book Controller's search action.
And run test cases following below
This gives user following output
To test applications, User can also use rake utility.
Here are few important rake commands.
Ruby on Rails 2.1 Related Interview Questions
|Python Interview Questions||HTML 5 Interview Questions|
|PHP Interview Questions||PostgreSQL Interview Questions|
|Ruby Interview Questions||Django Interview Questions|
|wxPython Interview Questions||MongoDB Interview Questions|
|Python Automation Testing Interview Questions||GitHub Interview Questions|
|Ruby Sinatra Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.