简体   繁体   中英

Rails: Failing test with an ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR

I have a pretty straightforward User model and want to test it:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :email

      t.timestamps null: false
    end
  end
end

And here is what I have in test/models/user_test.rb:

require 'test_helper'

class UserTest < ActiveSupport::TestCase

  test "shoudl always pass" do
    assert true
  end

end

The command rake test throws out the following error:

  1) Error:
UserTest#test_shoudl_always_pass:
ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR:  null value in column "name" violates not-null constraint
DETAIL:  Failing row contains (980190962, null, null, null, 2015-10-12 09:10:40, 2015-10-12 09:10:40).
: INSERT INTO "keywords" ("created_at", "updated_at", "id") VALUES ('2015-10-12 09:10:40', '2015-10-12 09:10:40', 980190962)

Why I'm getting such a strange error on an almost empty test case and how to fix it?

Solved! Commented the line fixtures :all in test_helper.rb

Usually updating your fixtures/users.yml can fix this. Either comment them out or make sure that they both create records with unique names

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM