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
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 .
:
class User < ActiveRecord::Base
validates :name, presence: true
end
, , :name nil, . .
, . @user.valid? ( ) false, :name . , , :
test "name should be present" do
@user.name = " "
assert_not @user.valid?
end
@user.valid? false, assert_not true, , , . : assert_not (is the user valid? no) == > assert_not(false) == > true. , " , ", true.
: :name, , , , , , db.
, :
test "name should be present" do
assert_not @user.valid?
end
, @user.valid? true ( " " ), assert_not @user.valid? false, , . : assert_not (is the user valid? yes) == > assert_not(true) == > false. , " , ", false. false ( / ), .
- , , , , , .