Rails:使用 ActiveRecord::StatementInvalid 测试失败:PG::NotNullViolation:错误

2 ruby testing tdd ruby-on-rails

我有一个非常简单的 User 模型并想测试它:

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

      t.timestamps null: false
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

这是我在 test/models/user_test.rb 中的内容:

require 'test_helper'

class UserTest < ActiveSupport::TestCase

  test "shoudl always pass" do
    assert true
  end

end
Run Code Online (Sandbox Code Playgroud)

该命令rake test抛出以下错误:

  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)
Run Code Online (Sandbox Code Playgroud)

为什么我在几乎空的测试用例上遇到如此奇怪的错误以及如何修复它?

小智 7

解决了!评论了fixtures :alltest_helper.rb 中的行