Ruby on Rails development from scratch (Windows) (22)-Test controller

Source: Internet
Author: User
Tags command line require ruby on rails

The last Test Modeul problem has not been resolved, but the following to continue, this time to test controller.

1. In the test\functional directory, rails has generated a corresponding test file for our controller, and note that Application_controller does not generate test files. Let's take control of the login Logincontroller as an example, open the LOGIN_CONTROLLER_TEST.RB, which reads as follows:

Require File.dirname (__file__) + '/... /test_helper '
require ' Login_controller '
# re-raise errors caught by the Controller.class Logincontroller ; def rescue_action (e) Raise e end; End
class Logincontrollertest < test::unit::testcase
def setup
@controller = logincontroller.new
@request = actioncontroller::testrequest.new
@response = actioncontroller::testresponse.new
End
# Replace This and your real tests.def Test_truth
asserts true
end
  

We see that in the Setup method, three objects @controller and @request and @response are defined, so that we can test without access to webserver or network.

2. Let's replace the Test_truth method with the following code:

def test_index
Get:index
assert_response:success
End

Where the Get method simulates a Web request, the requested action is index, and the response (response) is captured, and then the Assert_response assertion is used to determine whether the response was successful.

Run the test now: Depot>ruby test/functional/login_controller_test.rb

You'll see the test fails, the output from the command line:

Expected response to being a <:success>, but was <302>
1 tests, 1 assertions, 1 failures, 0 errors

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.