Although it is in the field, I think that development usually proceeds according to the following flow. (1) Design-> (2) Processing coding-> (3) Test coding-> (4) Unit testing, etc ...
But in the Rails tutorial, (1) Design → (2) Test coding → (3) Processing coding… It was explained in the flow. I learned a little, so I will post it as an article.
item | Contents |
---|---|
OS | aws #35-Ubuntu SMP(Cloud9) |
Ruby | ruby 2.6.3p62 (2019-04-16 revision 67580) |
Ruby On Rails | Rails 6.0.3 |
The following is an example of coding from the test source when adding a new about (overview) page to an app with routing and controller settings.
config/route.rb
Rails.application.routes.draw do
get 'static_pages/home'
get 'static_pages/help'
root 'static_pages#home'
* The routing of the overview page has not been set yet.
end
app/controller/static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
* The action on the summary page has not been set yet.
end
Step 1) Here, write a test for the newly added page in advance.
test/static_pages_controller_test.rb
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
1
2 test "should get home" do
3 get static_pages_home_url
4 assert_response :success
5 end
6
7 test "should get help" do
8 get static_pages_help_url
9 assert_response :success
10 end
11
12 ※~~~~From here, fill in the test on the summary page assuming.~~~~
13 test "should get about" do
14 get static_pages_about_url
15 assert_response :success
16 end
end
Step 2) Try to execute the test (1st time = error occurs because the process has not been described yet)
$rails test
Error:
StaticPagesControllerTest#test_should_get_about:
NameError: undefined local variable or method `static_pages_about_url'
..
Finished in 2.157942s, 1.3902 runs/s, 0.9268 assertions/s.
3 runs, 2 assertions, 0 failures, 1 errors, 0 skips
I got a error. </ font>
Step 3) From the above, the path does not pass, so review the routing.
config/route.rb
Rails.application.routes.draw do
get 'static_pages/home'
get 'static_pages/help'
get 'static_pages/about'← Added here ☆
root 'static_pages#home'
end
Step 4) Execute the test (2nd time = an error will occur on the assumption)
$ rails test
Error:
StaticPagesControllerTest#test_should_get_about:
AbstractController::ActionNotFound: The action 'about' could not be found for StaticPagesController…
3 runs, 2 assertions, 0 failures, 1 errors, 0 skips
I got an error again. </ font>
Step 5) From the above error, review the controller because the action is the cause.
app/controller/static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
def about ← Added here ☆
end
end
Step 6) Try running more tests (3rd time = what happens ...)
$ rails test
Error:
StaticPagesControllerTest#test_should_get_about:
ActionController::MissingExactTemplate: StaticPagesController#about is missing a template for request formats: ..
3 runs, 2 assertions, 0 failures, 1 errors, 0 skips
I got an error again. Next time, the wording seems to be different ... </ font>
Step 7) From the above error, it seems that the template is the cause, so add a view.
touch app/views/static_pages/about.html.erb
Step 8) Execute the test (4th time = normal test pass)
$ rails test
...
3 runs, 3 assertions, 0 failures, 0 errors, 0
It went well ... It's the basics of the basics, but I felt that it should be included in the refactoring. </ font>
Recommended Posts