[英]Rails throwing ActiveRecord::StatementInvalid PG::NotNullViolation: ERROR
[英]Rails: Failing test with an ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR
我有一个非常简单的用户模型,并想对其进行测试:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.timestamps null: false
end
end
end
这是我在test / models / user_test.rb中所拥有的:
require 'test_helper'
class UserTest < ActiveSupport::TestCase
test "shoudl always pass" do
assert true
end
end
命令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)
为什么在几乎空的测试用例中出现如此奇怪的错误,以及如何解决?
解决了! 在test_helper.rb中注释了行fixtures :all
通常,更新您的fixtures / users.yml可以解决此问题。 注释掉它们,或确保它们都创建具有唯一名称的记录
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.