简体   繁体   English

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

[英]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.

相关问题 Rails抛出ActiveRecord :: StatementInvalid PG :: NotNullViolation:错误 - Rails throwing ActiveRecord::StatementInvalid PG::NotNullViolation: ERROR ActiveRecord :: StatementInvalid:PG :: UndefinedTable:错误:在Rails查询中 - ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: in rails query ActiveRecord :: StatementInvalid:Rails中的PG :: UndefinedColumn - ActiveRecord::StatementInvalid: PG::UndefinedColumn in Rails Rails错误消息:ActiveRecord :: NotNullViolation - Rails Error Message: ActiveRecord::NotNullViolation Rails/Grape 应用程序中的排序错误 ActiveRecord::StatementInvalid PG::UndefinedColumn - Sorting error ActiveRecord::StatementInvalid PG::UndefinedColumn in Rails/Grape app ActiveRecord :: StatementInvalid:PG :: UndefinedTable:错误:与HABTM与rails关联的关系 - ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation with HABTM association with rails ActiveRecord :: StatementInvalid:PG :: UndefinedColumn:错误 - ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR ActiveRecord::StatementInvalid PG::UndefinedColumn: 错误 - ActiveRecord::StatementInvalid PG::UndefinedColumn: ERROR ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR: permission denied to create database in the rails 7 - ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR: permission denied to create database in the rails 7 Rails Rspec - ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: - Rails Rspec - ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM