ruby 代码
- database.yml
- test:
- adapter: sqlite3
- database: ":memory:"
- environment.rb
- config.active_record.schema_format = :ruby
- def in_memory_database?
- ENV["RAILS_ENV"] == "test" and
- ActiveRecord::Base.connection.class==ActiveRecord::ConnectionAdapters::SQLiteAdapter and
- Rails::Configuration.new.database_configuration['test']['database'] == ':memory:'
- end
- if in_memory_database?
- puts "creating sqlite in memory database"
- load "#{RAILS_ROOT}/db/schema.rb" # use db agnostic schema by default
- # ActiveRecord::Migrator.up('db/migrate') # use migrations
- end
本文介绍了一种在 Ruby on Rails 应用中为测试环境配置内存 SQLite 数据库的方法。通过检查环境变量和 ActiveRecord 连接适配器类型,确保在测试环境中使用内存数据库,并加载 schema.rb 文件来初始化数据库结构。
499

被折叠的 条评论
为什么被折叠?



