我目前正致力于设置activerecord_any_of
库,以便使用TravisCI跨多种数据库类型进行自动化测试。
目前我有以下内容:
在.travis.yml
language: ruby
script:
- bundle exec rake test
before_script:
- mysql -e 'create database activerecord_any_of_test;'
- psql -c 'create database activerecord_any_of_test;' -U postgres
before_install:
- gem install bundler # use the latest bundler, since Travis doesn't update for us
rvm:
- 1.9.3
- 2.0.0
- 2.1.1
- rbx
- jruby-19mode
env:
- DB=mysql
- DB=sqlite3
- DB=sqlite3mem
- DB=postgresql
gemfile:
- gemfiles/rails3.gemfile
- gemfiles/rails4.gemfile
- gemfiles/rails_edge.gemfile
matrix:
allow_failures:
- gemfile: gemfiles/rails_edge.gemfile
- env: DB=postgresql
- env: DB=mysql
database.yml
:
sqlite3:
adapter: <%= "jdbc" if defined? JRUBY_VERSION %>sqlite3
database: activerecord_any_of.sqlite3.db
sqlite3mem:
adapter: <%= "jdbc" if defined? JRUBY_VERSION %>sqlite3
database: ":memory:"
postgresql:
adapter: postgresql
encoding: unicode
database: activerecord_any_of_test
pool: 5
username: postgres
password:
min_messages: warning
mysql:
adapter: <%= defined?(JRUBY_VERSION) ? "jdbcmysql" : "mysql2" %>
host: localhost
username: root
password:
database: activerecord_any_of_test
encoding: utf8
## Add DB Configuration to run Oracle tests
oracle:
adapter: oracle_enhanced
host: localhost
username: activerecord_any_of_dev
password:
database: xe
我的test_helper.rb
plugin_test_dir = File.dirname(__FILE__)
require 'rubygems'
require 'bundler/setup'
require 'rails'
require 'active_record'
require 'activerecord_any_of'
require "rails/test_help"
require 'combustion/database'
require 'database_cleaner'
require 'pry'
require 'logger'
require 'yaml'
require 'erb'
require 'support/models'
ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log")
ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(plugin_test_dir + "/db/database.yml")).result)
ActiveRecord::Base.establish_connection(ENV["DB"] ||= "sqlite3mem")
ActiveRecord::Migration.verbose = false
Combustion::Database.create_database(ActiveRecord::Base.configurations[ENV["DB"]])
load(File.join(plugin_test_dir, "db", "schema.rb"))
ActiveSupport::TestCase.fixture_path = "#{plugin_test_dir}/fixtures"
ActiveSupport::TestCase.use_transactional_fixtures = true
ActiveSupport::TestCase.teardown do
unless /sqlite/ === ENV['DB']
Combustion::Database.drop_database(ActiveRecord::Base.configurations[ENV['DB']])
end
end
当我在TravisCI上运行测试时,在大多数情况下,sqlite
和sqlitemem
矩阵运行正常。但是,在针对MySQL运行时,我收到以下错误:
Mysql2::Error: Unknown database activerecord_any_of_test
当对PostgreSQL运行时,我收到错误:
ActiveRecord::StatementInvalid: PG::ConnectionBad: connection is closed: ROLLBACK
由于我正在创建数据库,所以我不明白为什么会有任何问题。据我所知,我已将database.yml
配置为使用正确的凭据。也许我使用Combustion::Database
错了?如何在测试中正确连接到我的数据库?
我已调整awesome_nested_set中使用的设置来设置activerecord_any_of的测试。在特拉维斯可以看到Failing tests。