[英]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: 这是我在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: 命令
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)
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 在test_helper.rb中注释了行
fixtures :all
Usually updating your fixtures/users.yml can fix this. 通常,更新您的fixtures / users.yml可以解决此问题。 Either comment them out or make sure that they both create records with unique names
注释掉它们,或确保它们都创建具有唯一名称的记录
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.