【问题标题】:How to make integrations tests in Rails afther create the model user with devise使用设计创建模型用户后如何在 Rails 中进行集成测试
【发布时间】:2020-03-08 00:04:41
【问题描述】:

我正在做一个集成测试,该测试在安装设计之前有效。现在为重复键抛出错误。试图插入用户。问题是我正在做和测试测试是针对一个甚至没有关联的类别模型。我留下代码和错误。谢谢

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

【问题讨论】:

  • 检查 test_helper.rb 是否创建用户?或者如果您正在使用夹具,则检查夹具/用户是否有用户使用相同的电子邮件
  • 非常感谢您的帮助。事实上,我有固定装置:所有,但在我的 users.yml 中我没有很好地配置用户。我将所做的更改留在答案中。

标签: ruby-on-rails ruby integration-testing


【解决方案1】:

感谢kevinluo201 我通过在 users.yml 中配置两个不同的电子邮件用户解决了我的问题 尽管在 test_helper.rb 中也注释了“fixtures: all”行。您也可以运行测试。

users.yml:

one:
 email: email1@example.com

two: 
 email: email2@example.com

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 2021-11-15
    相关资源
    最近更新 更多