自定义 TestTask 中的 ConnectionNotEstablished 错误
我正在尝试编写一个自定义 Rake 任务来对 lib 目录中的类执行一些测试。这适用于不需要任何模型的基本测试,但我需要使用一些模型进行实际测试。这是我第一次尝试更高级的 rake 使用,在经历了其他一些障碍之后,我陷入了 ConnectionNotEstablished 错误的困境。
这是 rake 任务:
Rake::TestTask.new(:test => 'db:test:prepare') do |test|
test.libs << 'test/sync'
test.test_files = Dir['test/sync/*_test.rb']
test.verbose = true
end
这是引发 ConnectionNotEstablished 异常的测试:
require 'rubygems'
require 'app/models/city'
require 'foo'
require 'test/unit'
class SyncTest < Test::Unit::TestCase
@@sync = Foo::Sync.new('test')
def test_cities
assert City.all.size == 2 # the exception is raised at this point
@@sync.cities
assert City.all.size == 102
end
end
这是实际工作的单元测试:
require 'test_helper'
class CityTest < ActiveSupport::TestCase
test "the truth" do
assert City.all.size == 2
end
end
我尝试使用从 ActiveSupport::TestCase
派生我的测试类而不是 Test ::Unit::TestCase
但它仍然引发 ConnectionNotEstablished 错误。我肯定做错了什么,有人能找到什么或告诉更好的方法吗?
I'm trying to write a custom Rake task to perform some tests for a class placed in the lib directory. This works for basic tests not requiring any models, but I need to actually test using some models. It's my first foray into more advanced rake usage and after going through some other hurdles I've got stuck on getting a ConnectionNotEstablished error.
Here's the rake task:
Rake::TestTask.new(:test => 'db:test:prepare') do |test|
test.libs << 'test/sync'
test.test_files = Dir['test/sync/*_test.rb']
test.verbose = true
end
Here's the test that raise the ConnectionNotEstablished exception:
require 'rubygems'
require 'app/models/city'
require 'foo'
require 'test/unit'
class SyncTest < Test::Unit::TestCase
@@sync = Foo::Sync.new('test')
def test_cities
assert City.all.size == 2 # the exception is raised at this point
@@sync.cities
assert City.all.size == 102
end
end
Here's a unit test that is actually working:
require 'test_helper'
class CityTest < ActiveSupport::TestCase
test "the truth" do
assert City.all.size == 2
end
end
I've tried using derive my test class from ActiveSupport::TestCase
instead of Test::Unit::TestCase
but it still raise a ConnectionNotEstablished error. I'm certainly doing something wrong, can anyone find what or tell of a better way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于找到了问题所在,我将前两个
require
调用替换为:并将测试目录添加到 TestTask 的 libs 属性中:
I've finally found out what the problem was, I've replaced the two first
require
call by:and added the test directory to the TestTask's libs attribute: