Ruby-on-rails Rails 5,Rspec:在架构中找不到环境数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38209186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:26:28  来源:igfitidea点击:

Rails 5, Rspec: Environment data not found in the schema

ruby-on-railsrspecrspec-railsruby-on-rails-5

提问by steel

After upgrading a Rails app to Rails 5, running RSpec tests gives me the following error:

将 Rails 应用程序升级到 Rails 5 后,运行 RSpec 测试会出现以下错误:

rails aborted!
ActiveRecord::NoEnvironmentInSchemaError: 

Environment data not found in the schema. To resolve this issue, run: 

    bin/rails db:environment:set RAILS_ENV=test

However, that bindoes not exist and I can't seem to generate it with bundle binstubs railsor with rake rails:update:bin.

但是,这bin不存在,我似乎无法使用bundle binstubs rails或使用rake rails:update:bin.

I have also tried:

我也试过:

rails db:environment:set RAILS_ENV=test
rake db:environment:set RAILS_ENV=test

There is a related issue on Github here.

Github 上有一个相关问题here

How can I address this error?

我该如何解决这个错误?

回答by steel

New Rails 5 command to generate binstubs:

用于生成 binstub 的新 Rails 5 命令:

rails app:update:bin

Allows me to run the solution as the error suggested:

允许我按照错误提示运行解决方案:

bin/rails db:environment:set RAILS_ENV=test

Tip from @max comment: If you are using database_cleanerand this error keeps popping up then change your config to:

来自@max 评论的提示:如果您正在使用database_cleaner并且此错误不断弹出,则将您的配置更改为:

DatabaseCleaner.clean_with(
  :truncation,
  except: %w(ar_internal_metadata)
)

回答by Mirror318

For me, this error was followed by a similar one asking for a migration.

对我来说,这个错误之后是一个类似的要求迁移的错误。

This did the trick: rails db:migrate RAILS_ENV=test

这做到了: rails db:migrate RAILS_ENV=test

回答by Exsemt

fix for jenkins before you drop database you should execute db:environment:setwith || true, so the command doesn't abort:

修复詹金斯之前你放弃你应该执行数据库db:environment:set|| true,因此命令不会中止:

bin/rails db:environment:set RAILS_ENV=test || true

回答by Cole Jurden

All of the above answers are correct, however, if you're in a more unique project such as developing a rails engine with a concept of a schema (hacky, I know) and your migration fails for some reason, you can re-run it without the check that throws this exception. Example:

以上所有答案都是正确的,但是,如果您在一个更独特的项目中,例如开发具有模式概念的 rails 引擎(我知道,hacky)并且您的迁移由于某种原因失败,您可以重新运行它没有引发此异常的检查。例子:

rake environment db:{drop,create,migrate} DISABLE_DATABASE_ENVIRONMENT_CHECK=1

rake environment db:{drop,create,migrate} DISABLE_DATABASE_ENVIRONMENT_CHECK=1

回答by Sergio Ocón-Cárdenas

For me, I had to do a mixture of things:

对我来说,我必须做多种事情:

bin/rails db:environment:set RAILS_ENV=test
bin/rails db:migrate RAILS_ENV=test

This would make things work, and then I had to review my migrations, I was adding a null:false into a relationship and that added a bug, the migration was cancelled and didn't finish

这会让事情顺利进行,然后我不得不检查我的迁移,我在关系中添加了一个 null:false 并添加了一个错误,迁移被取消并且没有完成

回答by Evmorov

I had to drop my database for tests and create it again:

我不得不放弃我的数据库进行测试并重新创建它:

  1. psql
  2. DROP DATABASE your_db_name_test;
  3. bundle exec rake db:create RAILS_ENV=test
  1. psql
  2. DROP DATABASE your_db_name_test;
  3. bundle exec rake db:create RAILS_ENV=test

After that the warning bin/rails db:environment:set RAILS_ENV=testdisappeared.

之后,警告bin/rails db:environment:set RAILS_ENV=test消失了。

回答by Artur Trzop

If you happen to see this error while using parallel_tests gem then you need to run below command for each DB. Just increase TEST_ENV_NUMBER.

如果您在使用 parallel_tests gem 时碰巧看到此错误,那么您需要为每个数据库运行以下命令。只需增加 TEST_ENV_NUMBER。

TEST_ENV_NUMBER=1 bin/rails db:environment:set RAILS_ENV=test
TEST_ENV_NUMBER=2 bin/rails db:environment:set RAILS_ENV=test

This helped me fix the problem when I was testing parallel_tests with knapsack_pro gem https://github.com/KnapsackPro/knapsack_pro-ruby#parallel_tests-with-knapsack_pro-on-single-ci-machine

当我使用 knapsack_pro gem https://github.com/KnapsackPro/knapsack_pro-ruby#parallel_tests-with-knapsack_pro-on-single-ci-machine测试 parallel_tests 时,这帮助我解决了这个问题