:対応するテーブルの移行とRailsのテーブルはnullとminitestモデルのテスト:私は新しいレール5アプリケーション開始している偽の制約
rails g model user
:
class CreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :email, null: false
t.string :password_digest, null: false
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.timestamps
end
end
end
rails new projectOne --api --database=postgresql
は、ユーザモデルを作成しました
次の最小テストを実行すると、
require 'test_helper'
class UserTest < ActiveSupport::TestCase
test "the truth" do
assert true
end
end
出力は次のとおりです。コンソールで
Error:
UserTest#test_the_truth:
ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR: null value in column "email" violates not-null constraint
DETAIL: Failing row contains (980190962, null, null, null, null, null, 2017-09-10 18:58:52.08302, 2017-09-10 18:58:52.08302).
: INSERT INTO "users" ("created_at", "updated_at", "id") VALUES ('2017-09-10 18:58:52.083020', '2017-09-10 18:58:52.083020', 980190962)
、私は空のUser
オブジェクトを作成することができますし、期待通りには、上記のエラーが発生するが、私は私のテスト内のオブジェクトを作成しようとしているものを理解していません。私は説明と、このテストに合格する方法のアドバイスを感謝します。