将 Devise Gem 添加到新的 Rails 项目会创建测试用户错误
Add Devise Gem to new Rails project creates test users errors
我以前没用过Devise。此时我的网站只有 bootstrap、HTML、样式和一些静态页面。我 using this Gemfile 我的所有测试都通过了,直到我 运行 rake db:migrate
。我运行按顺序执行这些步骤。
- 添加
gem 'devise'
、运行 bundle install
。 (测试绿色)
- 运行
rails generate devise:install
,添加闪光通知(测试绿色)
- 添加 action_mailer 默认行到每个文档的开发、测试和生产环境(测试绿色)
- 运行
rails generate devise user
(由于未决迁移,测试不会 运行)
- 根据文档检查新模型,但未更改默认值。
- 运行
rails db:migrate
(测试红色)运行宁这在我的数据库文件夹中创建了一个 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 元素是否存在以及链接是否有效之外,我还没有创建任何测试来做任何事情,但尽管如此......
似乎为了通过唯一性约束,我需要一个用户,但我还没有创建任何用户。它还与SQLite有关。在我目前的情况下,我怎样才能让这个测试通过呢?我基本上使用 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
您可以通过以下方式更新您的 test/fixtures/users.yml
文件来解决这个问题。
one:
email: "mail@mail.com"
#
two:
email: "mail_2@mail.com"
我以前没用过Devise。此时我的网站只有 bootstrap、HTML、样式和一些静态页面。我 using this Gemfile 我的所有测试都通过了,直到我 运行 rake db:migrate
。我运行按顺序执行这些步骤。
- 添加
gem 'devise'
、运行bundle install
。 (测试绿色) - 运行
rails generate devise:install
,添加闪光通知(测试绿色) - 添加 action_mailer 默认行到每个文档的开发、测试和生产环境(测试绿色)
- 运行
rails generate devise user
(由于未决迁移,测试不会 运行) - 根据文档检查新模型,但未更改默认值。
- 运行
rails db:migrate
(测试红色)运行宁这在我的数据库文件夹中创建了一个 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 元素是否存在以及链接是否有效之外,我还没有创建任何测试来做任何事情,但尽管如此......
似乎为了通过唯一性约束,我需要一个用户,但我还没有创建任何用户。它还与SQLite有关。在我目前的情况下,我怎样才能让这个测试通过呢?我基本上使用 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
您可以通过以下方式更新您的 test/fixtures/users.yml
文件来解决这个问题。
one:
email: "mail@mail.com"
#
two:
email: "mail_2@mail.com"