簡體   English   中英

使用設計創建模型用戶后如何在 Rails 中進行集成測試

[英]How to make integrations tests in Rails afther create the model user with devise

我正在做一個在安裝設計之前工作的集成測試。 現在為重復鍵拋出錯誤。 試圖插入用戶。 問題是我正在做和測試的測試是針對一個甚至沒有關聯的類別模型。 我留下代碼和錯誤。 謝謝

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”行。 您還可以運行測試。

用戶.yml:

one:
 email: email1@example.com

two: 
 email: email2@example.com

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM