Rails notes: Test

Source: Internet
Author: User
ArticleDirectory
    • Rails Test
    • Unit Test
    • Rails (upgrade problem)
    • Functional Test (Controller)
    • Mock object
    • Test driver
    • Rake tool support
    • Performance Testing
Rails Test
    • Model test => unit test
    • Controller test => functional
Unit Test

By default, all models are generated in test/unit/xxxx_test.rb.

Rake clone_structure_to_test # The data results in Dev can be automatically synchronized to the data in test.

Fixture

The database is not easy to test, because the data is valid for a long time and difficult to reproduce. All technologies similar to CSV import (which I currently use) must be used, while fixture is something similar to CSV files, but it is more closely integrated with rails.ConveniencesThis is also a successful attempt by rails to automatically control the initial state of data (such a facility is not seen in Java, but there is a bad dbunit... XML and XML)

The fixture structure is a file in yaml format.

Version_control_book: ID: 1 title: pragmatic version control Description: How to Use version control image_url: http ://... /sk_svn_small.jpg price: 29.95 date_available: 2005-01-26 00: 00: 00entity_name: ID: 2 Title: pragmatic project automation Description: How to automate your project image_url: http ://... /sk_auto_small.jpg price: 29.95 date_available:

The header marker version_control_book indicates the name of the record. The indent part consists of a hash as a record. fixture supports using the ERB command for some dynamic generation.

Fixture can be directly used in all testcase:FixnamesCommand to import data to the object, which is very convenient. The access method isFixname(: Entityname ),

The corresponding relationship here is product. RB => Products (DB teable) => products. yml => fixture: Products

Test_helper.rb

Test_helper is responsible for the initialization in the test environment. You can see all the modifications made here, such as adding a new assert method.

Rails (upgrade problem)

1 fixture upgrade

    • The records in fixture in old rails will automatically create @ var instance variables in the object, which can be accessed directly using @ version_control_book.
    • The new product uses products (: ersion_control_book) and does not automatically create instance variables.

2. Transaction Management upgrade

    • Old rails has no transactions by default, and every testcase will generate data again, which is slow
    • A new transaction is used. Each testcase starts a new transaction and rollback after the transaction is completed to avoid data deletion and re-import. (For MySQL, you need to use a table type that supports transactions, such as InnoDB)

As for how to make the new rails adopt the old method for testing, there is no such requirement yet. As rails1.0 becomes popular, it may not be used in the future.

Functional Test (Controller)

Controller testing capability

    • Check internal rails objects: Session, Flash, cookies, etc.
    • Imitate the browser for operations: Get post put Delete head
    • Check the server response: assert_redirected_to assert_response
    • Check view: assert_template
    • Resolution view: assert_tag

Although the function is powerful, the kiss principle should be used for writing and testing only when necessary.

Mock object

In Ruby, mock is very simple, just take the following steps

 
Require 'models/some_model 'class somemodel def mockedmethod (ARG)... endend

In this way, the original method is automatically overwritten to achieve the mock effect.

Test driver
    • Write test first
    • If there is a bug, write the test first. You can test the bug before modifying it.
    • If a class is hard to be tested, it indicates that there is a problem with the design.
Rake tool support

.. See the rails Tool

Performance Testing

First, generate data in batches through the dynamic function of Fixture

 
Order _: ID: Name: Fred Email: fred@flintstones.com address: 123 rockpile circle pay_type: Check

In the test method, run the test with benchmark: realltime do XXX end.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.