【问题标题】:UNIQUE constraint failed: users.email: INSERT INTO "users" ("created_at", "updated_at", "id") VALUES ()唯一约束失败:users.email: INSERT INTO "users" ("created_at", "updated_at", "id") VALUES ()
【发布时间】:2018-01-17 07:52:57
【问题描述】:

这是我的 static_pages_controller_test,

    require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest
  test "should get home" do
    get static_pages_home_url
    assert_response :success
  end

  test "should get help" do
    get static_pages_help_url
    assert_response :success
  end

  test "should get about" do
    get static_pages_about_url
    assert_response :success
  end
end

错误:

StaticPagesControllerTest#test_should_get_home: ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE 约束失败:users.email: INSERT INTO "users" ("created_at", “updated_at”、“id”)值('2018-01-17 05:49:27.300065'、'2018-01-17 05:49:27.300065', 298486374)

我将 user.yml 更改为,

one:
email: test1@test.com

two:
email: test2@test.com

错误:

StaticPagesControllerTest#test_should_get_about: ActiveRecord::Fixture::FormatError: 夹具键不是哈希: C:/Sites/IntenetChatSystem/test/fixtures/users.yml, 键: ["one", “电子邮件”,我是两个“]

我很困惑我该怎么办,请给一些建议

【问题讨论】:

  • 您可能会收到此错误,因为您的 users.yml 没有正确缩进。 guides.rubyonrails.org/testing.html "每个夹具都有一个名称,后跟一个以冒号分隔的键/值对的缩进列表。记录通常用空行分隔。"
  • 您的 user.yml 是否正确缩进?请确保一个以下的电子邮件应在内部缩进 1 个。这意味着 1 和 2 应该是 0 缩进,而 email 应该是 1。

标签: ruby-on-rails ruby-on-rails-3 unit-testing


【解决方案1】:

user.yml 文件的缩进必须修复:

one:
  email: test1@test.com

two:
  email: test2@test.com

错误消息本身就是一个很好的提示,“fixture key is not a hash: ... keys: ["one", "email",I'mtwo"]" 你可以在键列表中看到存在 oneemail 键。您应该拥有的只是 onetwo 键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-25
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 2020-10-22
    • 1970-01-01
    相关资源
    最近更新 更多