我正在执行一项集成测试,该测试在安装devise之前已经起作用。现在,对重复的键抛出错误。尝试插入用户。事实是,我正在做的测试是针对没有关联的Category模型进行的。我留下了代码和错误。谢谢
require 'test_helper'
class CreateCategoriesTest < ActionDispatch::IntegrationTest
test "get new category form and create category" do
get new_category_path
assert_template 'categories/new'
assert_difference 'Category.count', 1 do
post categories_path, params: { category: { name: "measure tension", description: "This is the Category for measure tension"} }
follow_redirect!
end
assert_template 'categories/index'
assert_match "measure tension", response.body
end
end
错误如下:
Running via Spring preloader in process 14544
Run options: --seed 57420
# Running:
E
Error:
CreateCategoriesTest#test_get_new_category_form_and_create_category:
ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: llave duplicada viola restricción de unicidad «index_users_on_email»
DETAIL: Ya existe la llave (email)=().
: DELETE FROM "categories";
DELETE FROM "users";
INSERT INTO "categories" ("id", "name", "description", "created_at", "updated_at") VALUES (980190962, 'MyString', 'MyText', '2020-03-06 20:25:00.457344', '2020-03-06 20:25:00.457344'), (298486374, 'MyString', 'MyText', '2020-03-06 20:25:00.457344', '2020-03-06 20:25:00.457344');
INSERT INTO "users" ("id", "email", "encrypted_password", "reset_password_token", "reset_password_sent_at", "remember_created_at", "created_at", "updated_at") VALUES (980190962, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, '2020-03-06 20:25:00.463209', '2020-03-06 20:25:00.463209'), (298486374, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, '2020-03-06 20:25:00.463209', '2020-03-06 20:25:00.463209')
Error:
CreateCategoriesTest#test_get_new_category_form_and_create_category:
NoMethodError: undefined method `each' for nil:NilClass
bin/rails test test/integration/create_categories_test.rb:5
Finished in 0.023791s, 42.0323 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
对kevinluo201的致谢我通过在users.yml中配置两个不同的电子邮件用户来解决了我的问题尽管也在test_helper.rb中注释了“ fixtures:all”这一行。您也可以运行测试。
The users.yml:
one:
email: [email protected]
two:
email: [email protected]