每个人!
我不确定这是一个错误,或者我不知道的东西,但是如果我尝试在 Rails 中使用不同的数据库在 AR 测试中测试 joins(),它就不会工作。它似乎适用于开发模式、控制台,但不适用于测试。所以我的测试失败了。:(
如果我在这里遗漏了什么,请告诉我吗?或者也许可以帮助我测试并确保这是一个错误?
好的,所以想法是这样的:我有 2 个数据库。对于每个数据库,我都有不同的模型集。在我的示例中,假设我在两个数据库上都有用户和办公室。所以我的 YML 文件是这样的:
common: &common
username: root
password:
host: 127.0.0.1
encoding: utf8
adapter: mysql2
development:
<<: *common
database: scopetest_devel
test: &test
<<: *common
database: scopetest_test
other_development:
<<: *common
database: othertest_devel
other_test: &other_test
<<: *common
database: othertest_test
这是我的测试课程
class User < ActiveRecord::Base
attr_accessible :name, :office_id, :office
belongs_to :office
end
class Office < ActiveRecord::Base
attr_accessible :name
has_many :users
end
class AnotherUser < ActiveRecord::Base
establish_connection "other_#{Rails.env}"
self.table_name = "users"
attr_accessible :name, :office_id, :office
belongs_to :office, class_name: "AnotherOffice"
end
class AnotherOffice < ActiveRecord::Base
establish_connection "other_#{Rails.env}"
self.table_name = "offices"
attr_accessible :name
has_many :users, class_name: "AnotherUser"
end
如您所见,它们是相同的。除了它们所属的表和数据库连接。
现在,这是我的测试文件:
require 'test_helper'
class UserTest < ActiveSupport::TestCase
test "scoping works" do
office = Office.create!(name: 'My Office')
user = User.create!(office: office, name: 'John Doe')
assert_equal 1, User.joins(:office).count
end
test "another db scoping doesnt" do
office = AnotherOffice.create!(name: 'My Office 2')
user = AnotherUser.create!(office: office, name: 'John Doe')
assert_equal 1, AnotherUser.joins(:office).count
end
end
现在,令人惊讶的是(也许对你来说不是),第一个测试将通过。第二个会失败。但是,在控制台上,如果我创建这些模型并执行 joins(),它将起作用。基本上,在测试模式下,在不同数据库上使用连接的任何范围都将返回 0 计数。现在,这是一个错误吗?:(欢迎任何愿意测试它的人!
这是:
导轨 3.2.11 Ruby 1.9.3-p194
Gemfile.lock:
GEM
remote: https://rubygems.org/
specs:
actionmailer (3.2.11)
actionpack (= 3.2.11)
mail (~> 2.4.4)
actionpack (3.2.11)
activemodel (= 3.2.11)
activesupport (= 3.2.11)
builder (~> 3.0.0)
erubis (~> 2.7.0)
journey (~> 1.0.4)
rack (~> 1.4.0)
rack-cache (~> 1.2)
rack-test (~> 0.6.1)
sprockets (~> 2.2.1)
activemodel (3.2.11)
activesupport (= 3.2.11)
builder (~> 3.0.0)
activerecord (3.2.11)
activemodel (= 3.2.11)
activesupport (= 3.2.11)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activeresource (3.2.11)
activemodel (= 3.2.11)
activesupport (= 3.2.11)
activesupport (3.2.11)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.4)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
railties (~> 3.2.0)
coffee-script (2.2.0)
coffee-script-source
execjs
coffee-script-source (1.4.0)
erubis (2.7.0)
execjs (1.4.0)
multi_json (~> 1.0)
hike (1.2.1)
i18n (0.6.1)
journey (1.0.4)
jquery-rails (2.2.0)
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
json (1.7.6)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.19)
multi_json (1.5.0)
mysql2 (0.3.11)
polyglot (0.3.3)
rack (1.4.4)
rack-cache (1.2)
rack (>= 0.4)
rack-ssl (1.3.3)
rack
rack-test (0.6.2)
rack (>= 1.0)
rails (3.2.11)
actionmailer (= 3.2.11)
actionpack (= 3.2.11)
activerecord (= 3.2.11)
activeresource (= 3.2.11)
activesupport (= 3.2.11)
bundler (~> 1.0)
railties (= 3.2.11)
railties (3.2.11)
actionpack (= 3.2.11)
activesupport (= 3.2.11)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (>= 0.14.6, < 2.0)
rake (10.0.3)
rdoc (3.12)
json (~> 1.4)
sass (3.2.5)
sass-rails (3.2.6)
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
sprockets (2.2.2)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
thor (0.17.0)
tilt (1.3.3)
treetop (1.4.12)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.35)
uglifier (1.3.0)
execjs (>= 0.3.0)
multi_json (~> 1.0, >= 1.0.2)
PLATFORMS
ruby
DEPENDENCIES
coffee-rails (~> 3.2.1)
jquery-rails
mysql2
rails (= 3.2.11)
sass-rails (~> 3.2.3)
uglifier (>= 1.0.3)