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

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

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

New Rails 5 command to generate binstubs:

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_cleaner and this error keeps popping up then change your config to:

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

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:

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


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

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