Ruby on Rails development from scratch (Windows) (28)-run test with rake

Source: Internet
Author: User
Tags command line root directory ruby on rails

Rake is a program written in Ruby and we use rake to read the rakefile. And Rakefile contains defined tasks, each with a name, and some of the tasks it relies on, and a set of actions to perform. Now let's take a look at the rake in Rails. When you create a project using rails ' generate scripts, you automatically generate a Rakefile file in the root directory of your project to help you accomplish a lot of tasks, and we can also use a command to see what features are available and execute on the rails command line: Depot >rake–tasks command, you will see the database, documents, tests, temporary files, and other related commands. Here are a few examples to illustrate:

To create a test database, we've already used the test in front of us:

Depot> Rake Clone_structure_to_test

Run Test for model:

Rake Test_units

To run the controller test:

Depot>rake test_functional

To run all tests, the rake command does not require a parameter:

Depot>rake

Sometimes we don't want to run so many tests at once, we can use Ruby's own commands instead:

Depot>ruby Test/functional/search_controller_test.rb-n Test_search

You can also:

Depot>ruby Test/functional/search_controller_test.rb-n/search/

Test_search and/search/are equivalent.

Of course, it's a good idea to create a script that runs automatically to run tests so that you can take advantage of the idle time of your machine and ensure that if your changes cause bugs, your tests can be captured in time. Damagecontrol can help you finish the work.

You can also use rake to count your source, including the number of model and controller, the number of methods, the number of lines, and so on, using the command:

Depot>rake Stats

The following output is obtained:

+----------------------+-------+-------+---------+---------+-----+-------+
| Name |  Lines | LOC | Classes | Methods | m/c | loc/m |
+----------------------+-------+-------+---------+---------+-----+-------+
|  Controllers |  251 |    213 |   5 |  29 |   5 | 5 |
|  Helpers |  14 |   13 |    0 |  1 |  0 | One |
|  Models |  113 |    101 |   6 |  16 |   2 | 4 |
|   Libraries |   0 |    0 |    0 |  0 |   0 | 0 |
|   Components |   0 |    0 |    0 |  0 |   0 | 0 |
|   Integration Tests |   0 |    0 |    0 |  0 |   0 | 0 |
|  Functional Tests |  241 |    192 |   8 |  30 |   3 | 4 |
|  Unit Tests |  88 |    74 |   4 |  11 |   2 | 4 |
+----------------------+-------+-------+---------+---------+-----+-------+
|  Total |  707 |   593 |   23 |  87 |   3 | 4 |
+----------------------+-------+-------+---------+---------+-----+-------+
code loc:327 Test loc:266 code to T EST ratio:1:0.8

Well, it's good, at least it's convenient.

From the table above we saw that we had written some test code, but how can we guarantee the coverage of the test? There is a tool ruby Coverage,rails does not include it, and using Ruby Coverage you can generate an HTML-formatted report that contains the percentage of test coverage and which line of code has not been tested.

About Ruby Coverage, you can download it here, download it and unzip it, and in the Rails command line, which is our usual depot directory, execute the command in sequence:

Ruby Z:\TOOLS\COVERAGE-0.2\COVERAGE-0.2\INSTALL.RB Config

Ruby Z:\TOOLS\COVERAGE-0.2\COVERAGE-0.2\INSTALL.RB Setup

Ruby Z:\tools\coverage-0.2\coverage-0.2\install.rb Install

The blue one is the coverage installation file, and after executing the above command, the Ruby coverage installation is complete, and now we can use it to generate the test coverage report and run the command:

Ruby-rcoverage test/functional/search_controller_test.rb

Compared with our previous test, just add the middle blue part, after completion, in the depot directory will have a coverage directory, stored the generated HTML file, open to see, the coverage of the code will be marked with blue, not covered by the code will be gray.

In this article we have a rough introduction to rake command and Ruby coverage, next time we look at the performance test.

Related Article

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.