【问题标题】:Unit tests cannot find tables单元测试找不到表
【发布时间】:2012-05-21 22:21:44
【问题描述】:

我正在 Rails 2.3 上运行单元测试,当我运行 rake test:units 时,我不断收到这些错误:

10) Error:
test_the_truth(BrokerTest):
ActiveRecord::StatementInvalid: Mysql::Error: Table 'shadow_test.users' doesn't exist: DELETE FROM `users`
    /Library/Ruby/Gems/1.8/gems/after_commit-1.0.10/lib/after_commit/connection_adapters.rb:14:in `transaction'

我在此之前运行了rake db:test:load,它创建了表格。但是,当我在运行单元测试命令后检查mysql 时,确实缺少这些表。在准备单元测试的过程中,有些东西会导致表格丢失。这是运行测试的调试日志:

WARNING: 'task :t, arg, :needs => [deps]' is deprecated.  Please use 'task :t, [args] => [deps]' instead.
    at /Users/me/sources/shadow/lib/tasks/turk.rake:53
** Invoke test:units (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment 
** Execute db:test:purge
** Execute db:test:load
** Invoke db:schema:load (first_time)
** Invoke environment 
** Execute db:schema:load
** Execute test:units
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -I"lib:test" -I"/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib" "/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb" "test/unit/**/*_test.rb" 
/Library/Ruby/Gems/1.8/gems/bundler-1.1.3/lib/bundler/runtime.rb:211: warning: Insecure world writable dir /usr/local/git/bin in PATH, mode 040777
NOTE: Gem.source_index is deprecated, use Specification. It will be removed on or after 2011-11-01.
Gem.source_index called from /Library/Ruby/Gems/1.8/gems/rails-2.3.14/lib/rails/gem_dependency.rb:21.
WARNING: using the built-in Timeout class which is known to have issues when used for opening connections. Install the SystemTimer gem if you want to make sure the Redis client will not hang.
/Users/me/sources/shadow/test/unit/../test_helper.rb:36: warning: already initialized constant BEANSTALK
Loaded suite /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/rake_test_loader
Started
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
Finished in 3.389328 seconds.

更多背景知识:我正在使用 Octopus 进行分片,这可能会以某种方式干扰事物??

【问题讨论】:

    标签: mysql ruby-on-rails rake testunit octopus


    【解决方案1】:

    使用Octopus 数据库分片gem 在测试用例准备方面存在一些问题。请参阅 GitHub 问题:https://github.com/tchandy/octopus/issues/31

    您有几个选项,其中最简单的方法是为您的开发环境禁用Octopus。从您的shards.yml 文件中删除“-development”。

    否则,您可以通过添加任务来修复问题:

     task :reconnect_octopus do
       if ActiveRecord::Base.connection.is_a?(Octopus::Proxy)
         ActiveRecord::Base.connection.initialize_shards(Octopus.config)
       end
     end
     task :'db:test:clone_structure' => :reconnect_octopus
    

    或者对于 Rails 3:

     task :reconnect_octopus do
       if ActiveRecord::Base.connection.is_a?(Octopus::Proxy)
         ActiveRecord::Base.connection.initialize_shards(Octopus.config)
       end
     end
     task :'db:structure:load' => :reconnect_octopus
    

    这应该可以解决您上面列出的问题。编码愉快!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 2016-08-02
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多