Ruby on rails RubyonRails测试-ActiveRecord::RecordNotUnique:PG::UniqueViolation:ERROR:重复键值违反了唯一约束;xxx_xxxx_xxxx_pkey“;
我有以下型号。我正在使用Rails 6.0.3.2(Ruby 2.7.1p83)并使用Minitest作为测试引擎Ruby on rails RubyonRails测试-ActiveRecord::RecordNotUnique:PG::UniqueViolation:ERROR:重复键值违反了唯一约束;xxx_xxxx_xxxx_pkey“;,ruby-on-rails,ruby,postgresql,testing,Ruby On Rails,Ruby,Postgresql,Testing,我有以下型号。我正在使用Rails 6.0.3.2(Ruby 2.7.1p83)并使用Minitest作为测试引擎 class AuthContentType < ApplicationRecord has_many :auth_permissions end 我可以得到帮助吗?我一直在尝试执行一些建议的解决方案。但这对我没有帮助 解决方案:1(在控制台中运行) 我的测试/测试助手.rb ENV['RAILS_ENV'] ||= 'test' require_relative '..
class AuthContentType < ApplicationRecord
has_many :auth_permissions
end
我可以得到帮助吗?我一直在尝试执行一些建议的解决方案。但这对我没有帮助
解决方案:1(在控制台中运行)
我的测试/测试助手.rb
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
class ActiveSupport::TestCase
# Run tests in parallel with specified workers
parallelize(workers: :number_of_processors, with: :threads)
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
class ActiveSupport::TestCase
# Run tests in parallel with specified workers
# parallelize(workers: :number_of_processors, with: :threads)
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
我的测试\controllers\converge\u controller\u test.rb
require 'test_helper'
class ConvergeControllerTest < ActionDispatch::IntegrationTest
test "should get home" do
#get converge_home_url
get home_url
#assert_response :success
assert_redirected_to new_user_session_url
end
end
require 'test_helper'
class GdControllerTest < ActionDispatch::IntegrationTest
test "should get home" do
get gd_home_url
#assert_response :success
assert_redirected_to new_user_session_url
end
end
需要“测试助手”
类ConvergeControllerTest
我的测试\controllers\gd\u controller\u test.rb
require 'test_helper'
class ConvergeControllerTest < ActionDispatch::IntegrationTest
test "should get home" do
#get converge_home_url
get home_url
#assert_response :success
assert_redirected_to new_user_session_url
end
end
require 'test_helper'
class GdControllerTest < ActionDispatch::IntegrationTest
test "should get home" do
get gd_home_url
#assert_response :success
assert_redirected_to new_user_session_url
end
end
需要“测试助手”
类GdControllerTest
数据库中可能有唯一的索引。您可以添加一个任务并运行 rake数据库:更正顺序id rails g任务数据库更正\u seq\u id 然后生成一个文件,如lib/tasks/database.rake 你应该把这个街区:
namespace :database do
desc "Correction of sequences id"
task correction_seq_id: :environment do
ActiveRecord::Base.connection.tables.each do |t|
ActiveRecord::Base.connection.reset_pk_sequence!(t)
end
end
end
知道我的机器有Windows操作系统,我通过执行以下操作解决了问题: 我把这句话注释掉了
parallelize(workers: :number_of_processors, with: :threads)
从我的test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
class ActiveSupport::TestCase
# Run tests in parallel with specified workers
parallelize(workers: :number_of_processors, with: :threads)
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
class ActiveSupport::TestCase
# Run tests in parallel with specified workers
# parallelize(workers: :number_of_processors, with: :threads)
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
该错误表明,从加载测试装置时,您违反了表auth\u content\u types上的主键约束。通过从设备标签构建校验和,设备使用稳定的ID。您可能多次加载装置,或者清理不起作用。请发布您的
lib/test\u helper.rb
和test/controllers/gd\u controller\u test.rb
,他们需要找到错误的确切来源。我添加了“test\u helper.rb”和test/controllers/gd\u controller\u test.rb脚本here@Gizemsoylutr回溯显示问题发生在加载测试夹具时,具有稳定(固定)ID的。加载夹具后,rails已经重置了PK序列以避免冲突:Windows上应该支持并行测试,并且由于rails应该为每个工作者创建不同的测试数据库,因此不应该有任何冲突。也许这是一个并行测试的bug,只发生在windows上。您可以尝试在rails问题跟踪程序中报告问题:
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
class ActiveSupport::TestCase
# Run tests in parallel with specified workers
# parallelize(workers: :number_of_processors, with: :threads)
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end