[Ruby] In implementing, after writing the test, code the process

2 minute read

background

There are some on-site situations, but I think that we usually proceed with the development in the following flow. (1) Design → (2) Process coding → (3) Test coding → (4) Unit test, etc.

But in the Rails tutorial, (1) Design → (2) Test coding → (3) Processing coding… It was explained in the flow. I also learned a little, so I will post it as an article.

environment

| Item | Content | |:—————–|:——————:| | OS | aws #35-Ubuntu SMP(Cloud9)| | Ruby | ruby 2.6.3p62 (2019-04-16 revision 67580) | | Ruby On Rails | Rails 6.0.3 |

Compatible

The following is an example of coding from a test source when adding a new about page to an app that has routes and controllers set.

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 overview page has not been set yet.
end

Step 1) Here, describe the 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 ~~~~ Fill in the test on the summary page from here. ~~~~
13 test "should get about" do
14 get static_pages_about_url
15 assert_response :success
16 end

end

Step 2) Execute the test (1st time = error 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 an error.

Step 3) From the above, since the path is not through, 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 = error as expected)

$ 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.

Step 5) Review the controller from the above error 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) Run the test further (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. The next word seems to be different...

Step 7) From the above error, the template seems to be 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 kept, including refactoring.

References

Tutorial. Start from testing