Rails:旧的,不存在的数据库表导致测试错误

时间:2011-06-02 23:39:47

标签: ruby-on-rails unit-testing

我一直在尝试在我最近负责的Rails应用程序中运行一个“真实”单元测试mac_test.rb。它看起来像这样:

    require 'test_helper'

    class MacTest < ActiveSupport::TestCase
      # Replace this with your real tests.
      test "the truth" do
        assert true
      end
    end

我已经rake db:test:prepare运行了,我正在尝试使用ruby -I test test/unit/mac_test.rb运行测试。

但是,我得到以下缩写错误输出:

    Loaded suite test/unit/mac_test
    Started
    EEEEEEEEEEEEEEE
    Finished in 0.377261 seconds.

      1) Error:
    test_datas(ActionController::IntegrationTest):
    ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: vendors: DELETE FROM "vendors" WHERE 1=1


      2) Error:
    testjigs(ActionController::IntegrationTest):
    ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: vendors: DELETE FROM "vendors" WHERE 1=1


      3) Error:
    test_datas(ActionController::TestCase):
    ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: vendors: DELETE FROM "vendors" WHERE 1=1

    <more errors here>

    15 tests, 0 assertions, 0 failures, 15 errors

所有15个错误都与前三个类似 - 他们抱怨缺少供应商表。供应商显然曾经存在 - 有一些剩余的文件,比如vendor_helper,在routes.rb文件中也有map.resources :vendor。我删除了旧的供应商文件并删除了map.resources:vendor line。

因此没有模型,视图,控制器,测试,数据库表或任何命名供应商/供应商的东西。使用TextMate搜索“供应商”的项目(不区分大小写),只有一个“供应商”不在注释,日志文件,README或boot.rb文件中,这是在Capfile中。 (我认为不应该影响测试?)

我尝试使用rake db:reset查看它是否是一些奇怪的数据库事物,但它仍在爆炸。

如果我尝试使用rake test:units --trace,(只是为了获得跟踪,我不知道如何通过单独测试获得跟踪)我得到缩写输出:

    ** 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
    /usr/local/bin/ruby -I"lib:test" "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/unit/auth_code_test.rb" "test/unit/cal_data_test.rb" "test/unit/calibration_test.rb" "test/unit/helpers/auth_codes_helper_test.rb" "test/unit/helpers/jn_macs_helper_test.rb" "test/unit/jn_mac_test.rb" "test/unit/log_entry_test.rb" "test/unit/mac_test.rb" "test/unit/option_test.rb" "test/unit/q_test_test.rb" "test/unit/serial_test.rb" "test/unit/source_test.rb" "test/unit/test_data_test.rb" "test/unit/testjig_test.rb" 
    Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader
    Started
    EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
    Finished in 1.06271 seconds.

      1) Error:
    test_datas(ActionController::IntegrationTest):
    ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: vendors: DELETE FROM "vendors" WHERE 1=1

    <more similar errors>

    52 tests, 0 assertions, 0 failures, 52 errors
    rake aborted!
    Command failed with status (1): [/usr/local/bin/ruby -I"lib:test" "/usr/loc...]
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:995:in `sh'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1010:in `call'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1010:in `sh'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1094:in `sh'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1029:in `ruby'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1094:in `ruby'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/testtask.rb:117:in `define'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1112:in `verbose'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/testtask.rb:102:in `define'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
    /usr/local/lib/ruby/1.8/monitor.rb:242:in `synchronize'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
    /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31
    /usr/local/bin/rake:19:in `load'
    /usr/local/bin/rake:19

我对如何让它不爆炸感到茫然。任何人都可以帮我找到让Rails意识到不再有任何“供应商”的方法吗?

编辑:我正在使用Rails 2.3.2和ruby 1.8.7(2009-06-12 patchlevel 174)[i686-darwin9.7.0]

2 个答案:

答案 0 :(得分:23)

在test文件夹中查找名为vendors.yml的fixture文件。如果它存在并且您没有供应商表,那么您将会收到这些错误,因为默认情况下,Rails会尝试使用该文件中的数据填充供应商表。

答案 1 :(得分:1)

您的{RAILS_ROOT} /db/schema.rb文件可能仍然包含有关先前供应商表的一些信息。该文件中的信息用于构建测试环境。如果它确实包含供应商表信息,您可以运行rake db:schema:dump来刷新该文件,然后再次尝试测试。