Tutorial by Michael Hartl Rails: assert_not does the exact opposite of what you need and I don't understand why

tl; dr Valid names are not written to the database because the test fails, and invalid names are written to the database as the test passes.

Edit: To clarify the project and my question in general: as described in the book, this user model is configured as the initial stages, so that the website user can ultimately log into the Website system. The database columns will be β€œname” and β€œemail”, and each row will be a single user (provided that the username and email address were valid). I edited my initial post below for further clarification, and all changes are in italics.

In addition, please answer only if you can explain the code as it is in my message - do not offer to add additional code to make it work. The tutorial I am working on claims that this code should work as it is, but it seems to appreciate otherwise that it should. Finally, if you know other links that explain this in more detail, this is helpful; however, I already read the apidock , RoR API , and most of the SO links that appear in a Google search, and none of them helped explain this problem.

I am working on the Michael Hartl Ruby on Rails Tutorial. I'm in chapter six, working with validation checks, and I'm stuck on checking for a name. It seems that he is doing exactly the opposite of what the textbook says should be done (for example, checking an invalid name record), although I followed the textbook exactly. I also searched the Internet and SO for more details on how it works assert_not, but cannot find useful information. Here is the process that I went through.

  • Add a test that we know will fail:

    require 'test_helper'
    
    class UserTest < ActiveSupport::TestCase
    
      def setup
        @user = User.new(name: "Example User", email: "user@example.com")
      end
    
      # resolves to true and passes
      test "should be valid" do
       assert @user.valid?
      end
    
      test "name should be present" do
        @user.name = "     "
        assert_not @user.valid?
      end
    
    end
    

. , , assert_not @user.valid? : @user.valid? true, , . assert_not, false .

  1. :

    class User < ActiveRecord::Base
      validates :name, presence: true
    end
    

, , :name nil, . .

  1. , . @user.valid? ( ) false, :name . , , :

    test "name should be present" do
        @user.name = "   "
        # => name: "   " email: "example@example.com"
        assert_not @user.valid? 
        # => assert_not false => true
    end
    

@user.valid? false, assert_not true, , , . : assert_not (is the user valid? no) == > assert_not(false) == > true. , " , ", true.

: :name, , , , , , db.

  1. , :

    test "name should be present" do
    # => name: "Example User" email: "example@example.com"
        assert_not @user.valid? 
        # assert_not true => false
    end
    

, @user.valid? true ( " " ), assert_not @user.valid? false, , . : assert_not (is the user valid? yes) == > assert_not(true) == > false. , " , ", false. false ( / ), .

- , , , , , .

+4
2

: name, , ...

assert_not true ( , ), @user.valid? false. , 3 : " , . / , true . .

+3

, "",

  class User < ActiveRecord::Base
   validates :name, presence: true, allow_blank: false
   validate :name_of_valid

   def name_of_valid
    self.errors.add :base, 'My string can not be empty' if self.name == "   " 
   end
end

test "name should be present" do
    @user.name = "   "
    # => name: "   " email: "example@example.com"
    assert_not @user.valid? 
    # => assert_not true 
end
0

Source: https://habr.com/ru/post/1605672/


All Articles