自定义 TestTask 中的 ConnectionNotEstablished 错误

发布于 2024-10-14 20:08:19 字数 1072 浏览 1 评论 0原文

我正在尝试编写一个自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪 2024-10-21 20:08:20

我终于找到了问题所在,我将前两个 require 调用替换为:

require 'test_helper'

并将测试目录添加到 TestTask 的 libs 属性中:

test.libs << 'test'

I've finally found out what the problem was, I've replaced the two first require call by:

require 'test_helper'

and added the test directory to the TestTask's libs attribute:

test.libs << 'test'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文