Rails minitest model test with table null:false constraint -
i have started new rails 5 application:
rails new projectone --api --database=postgresql
created user model:
rails g model user
with corresponding table migration:
class createusers < activerecord::migration[5.0] def change create_table :users |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
when run following minitest test:
require 'test_helper' class usertest < activesupport::testcase test "the truth" assert true end end
the output is:
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 "users" ("created_at", "updated_at", "id") values ('2017-09-10 18:58:52.083020', '2017-09-10 18:58:52.083020', 980190962)
in console, can create empty user
object , expected receive above error, not understand trying create object within test. i'd appreciate explanation , advice of how test pass.
the command bundle exec rails g model somemodel
invoking bunch of generators include test generator. test generators generate test template , fixtures:
fixtures fancy word sample data. fixtures allow populate testing database predefined data before tests run. fixtures database independent , written in yaml. there 1 file per model.
the minitest trying load environment , populate test database fixtures, fails in case. look's have invalid fixtures. in order resolve issue check forlder test/fixtures
, fix or delete fixtures.
read more testing in rails
.
Comments
Post a Comment