0

我有一个奇怪的错误。当我尝试运行单元测试时,它给了我以下错误:

test_the_truth(UpdateMainTest):
 ActiveRecord::StatementInvalid: Could not find table 'blacklists'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:472:in `table_structure'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:346:in `columns'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:660:in `column_names'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:652:in `timestamp_column_names'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:585:in `block in table_rows'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:579:in `each'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:579:in `map'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:579:in `table_rows'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:494:in `block (3 levels) in create_fixtures'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:492:in `each'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:492:in `block (2 levels) in create_fixtures'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:491:in `block in create_fixtures'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:168:in `disable_referential_integrity'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:476:in `create_fixtures'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:895:in `load_fixtures'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:849:in `setup_fixtures'
/var/lib/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:407:in `_run__379848442555604621__setup__3487204981098397389__callbacks'
/var/lib/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
/var/lib/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_setup_callbacks'
/var/lib/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
/var/lib/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/testing/setup_and_teardown.rb:35:in `run'

这是我的黑名单模型:

class Blacklist < ActiveRecord::Base
  attr_accessible :abbreviation, :name
  self.table_name = "blacklist"
  validates :abbreviation, :presence => :true, :uniqueness => :true
  validates :name, :presence => :true
end

该架构还正确列出了该表:

  create_table "blacklist", :force => true do |t|
    t.string   "name"
    t.string   "abbreviation"
    t.datetime "created_at",   :null => false
    t.datetime "updated_at",   :null => false
  end

我不止一次运行 rake db:test:prepare,但错误仍然存​​在。当我使用 rails 控制台时,我可以创建黑名单对象而不会出错。知道我能做什么吗?

4

3 回答 3

0

使用复数形式的符号,表名应为复数:

self.table_name = :blacklists

用复数形式创建表名:

create_table "blacklists", :force => true do |t|
    t.string   "name"
    t.string   "abbreviation"
    t.datetime "created_at",   :null => false
    t.datetime "updated_at",   :null => false
  end
于 2013-07-24T20:40:38.837 回答
0

感谢您的所有帮助。

我无法真正追踪问题的根源,但是当我注释掉 test_helper.rb 中加载所有固定装置的行时,错误没有再次出现。

于 2013-07-29T19:05:53.420 回答
0

如果仅发生在您的单元测试中,则测试数据库可能已过时。你应该尝试重建它rake db:migrate db:test:clone

于 2013-07-25T18:00:15.327 回答