将Devise Gem添加到新的Rails项目会产生测试用户错误

时间:2017-01-01 22:33:27

标签: ruby-on-rails devise ruby-on-rails-5

我之前没有使用过Devise。我的网站只是bootstrap,HTML,样式和一些静态页面。我是using this Gemfile所有测试都通过,直到我运行rake db:migrate。我按此顺序运行了这些步骤。

  • 添加gem 'devise',运行bundle install。 (测试绿色)
  • 运行rails generate devise:install,添加flash通知(测试绿色)
  • 将action_mailer默认行添加到每个文档的开发,测试和生产环境中(测试绿色)
  • 运行rails generate devise user(测试因未决的迁移而无法运行)
  • 检查每个文档的新模型,但未对默认值进行任何更改。
  • 运行rails db:migrate(测试红色)运行此操作在我的db文件夹中创建了一个test.sqlite3文件

我现在收到此错误:

ActiveRecord::RecordNotUnique: ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: users.email: INSERT INTO "users" ("created_at", "updated_at", "id") VALUES ('2017-01-01 22:14:19.589023', '2017-01-01 22:14:19.589023', 298486374)

我的所有测试。这很奇怪,因为除了检查HTML元素是否存在以及链接是否有效之外,我还没有创建任何可以执行任何操作的测试...

似乎为了传递uniquiness约束我需要一个用户,但我没有创建任何。它也与SQLite有关。我怎样才能通过Devise来完成这个测试。我基本上使用Hartl教程作为指导。我记得有一个步骤,我们创建假用户,但这不是那个时刻的目的?

require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest

  def setup
    @title = "FFP"
  end

  test "should get root" do
    get root_url
    assert_response :success
    assert_select "title", @title
  end
end    

目前这是我的架构。

 ActiveRecord::Schema.define(version: 20170101224251) do

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "provider"
    t.string   "uid"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

end

1 个答案:

答案 0 :(得分:3)

您可以通过以下方式更新test/fixtures/users.yml文件来解决此问题。

one:
  email: "mail@mail.com"
#
two:
  email: "mail_2@mail.com"