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