Rails 6最小测试ActiveRecord :: InvalidForeignKey:PG :: ForeignKeyViolation

问题描述

我在Rails中运行测试时遇到问题,我已将postgres用户设置为超级用户,但仍然出现错误

Minitest::UnexpectedError:         ActiveRecord::InvalidForeignKey: PG::Foreignkeyviolation: ERROR:  insert or update on table "mobile_applications" violates foreign key constraint "fk_rails_5674f4d4a5"
        DETAIL:  Key (tenant_id)=(839733711) is not present in table "tenants".

随机失败的测试之一:

  test 'mobile app should allow valid creation' do
    tenant = tenants(:test_tenant)
    user_tenant = user_tenants(:test_user_tenant)
    mobile_app = MobileApplication.new(tenant: tenant,user_tenant: user_tenant,name: 'My New app',platform: :iOS,deploy_type: :Beta)
    assert mobile_app.save
  end

在模型测试(ActiveSupport :: TestCase)和葡萄API的某些集成测试(Actiondispatch :: IntegrationTest)中都可以重复使用这些夹具。

如果我运行Rails测试/模型或Rails测试/控制器,总是成功,但是如果我运行Rails测试,它每次都会以新的随机方式失败。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)