[RUBY] Testing for Error Messages: Rails Tutorial Notes-Chapter 7

Rails Tutorial 6.0 Chapter 7 7.3.4 Test at the time of failure Record of stumbling in the last exercise

Exercise content

"Write a test for the error message implemented in Listing 7.20. I'll leave it to you to test it in detail. I've provided a template for Listing 7.25 for your reference."

answer

Use assert_select It's a good idea to check the HTML structure for improper login-specific elements.

Contents of the partial created immediately before (This content is inserted in new.html.erb)

ruby:/sample_app/app/views/shared/_error_messages.html.erb


<% if @user.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-danger">
      The form contains <%= pluralize(@user.errors.count, "error") %>.
    </div>
    <ul>
    <% @user.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

If @ user.errors exists, A div tag containing a specific class ("alert" & "alert-danger"), id ("error_explanation") is expanded

As long as this is included in the final HTML passed to the browser, assert_select'Selector (class: div.class, id: div # id)' Confirm it with

The completed form is as follows

/sample_app/test/integration/users_signup_test.rb


require 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest

  test "invalid signup information" do
    assert_no_difference 'User.count' do
      post users_path, params: { user: { name: "",
                                         email: "user@invalid",
                                         password: "foo",
                                         password_confirmation: "bar"} }
    end
    assert_template 'users/new'
    assert_select 'div.alert'
    assert_select 'div.alert-danger'
    assert_select 'div#error_explanation'
  end

end

The structure verified by assert_select, Visually shown in Google Chrome developer tools スクリーンショット 2020-06-04 17.50.58.png

that's all

What I don't understand (for self-learning)

Use "" and "" properly I feel like "" is preferred when explicitly indicating that it is a string, The selector is specified as'''

Recommended Posts

Testing for Error Messages: Rails Tutorial Notes-Chapter 7
11.1 AccountActivations Resources: Rails Tutorial Notes-Chapter 11
[Rails] Workaround for classes automatically generated by devise error messages
[Rails] How to display error messages for comment function (for beginners)
[Rails] Japanese localization of error messages
rails tutorial chapter 10 summary (for self-learning)
Japanese localization of error messages (rails)
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
[Rails] How to display error messages individually
[Ruby / Rails] Mechanism for retrying Thread Error
11.2 Send Account Activation Email: Rails Tutorial Notes--Chapter 11
The road to Japaneseizing Rails devise error messages
rails tutorial Chapter 6
[Ruby on Rails] How to display error messages
Rails tutorial test
rails tutorial Chapter 1
Rails tutorial memorandum 1
Rails tutorial memorandum 2
Rails error messages cannot be translated into Japanese
rails tutorial Chapter 7
rails tutorial Chapter 5
rails tutorial Chapter 10
rails tutorial Chapter 9
rails tutorial Chapter 8
[Rails] How to get success and error messages
Start Rails Tutorial
[Beginner] Rails Tutorial
[Ruby on Rails] Individual display of error messages
I changed the way Rails tutorials run: Rails Tutorial Notes-Chapter 9
Rails uninitialized constant A simple checklist for error resolution
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Japaneseization of Rails error messages [devise / Form objects, etc.]
[Rails Tutorial] "NoMethodError (undefined method` activation_digest ='for # <User: 0x00000003156938>
What should I use for the testing framework [Rails]
[Note] Rails error list
About Ruby error messages
Catch Rails Routing Error
Rails Tutorial Chapter 5 Notes
Rails Tutorial Chapter 10 Notes
Rails Tutorial Chapter 3 Notes
Rails Tutorial Chapter 3 Learning
[Rails error] unexpected tIDENTIFIER
Mac Rails Install Error
[Rails] Learning with Rails tutorial
rails heroku error log
Rails Tutorial Memorandum (Chapter 3, 3.1)
rails error resolution summary
Rails Tutorial Chapter 4 Notes
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Rails Tutorial Chapter 8 Notes
rails tutorial fighting notes Ⅲ
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
Tutorial to create a blog with Rails for beginners Part 1
SassC :: SyntaxError in Pages # index Remedy for rails5 Intermediate Tutorial