Rails - rake测试失败

时间:2013-02-23 16:31:25

标签: ruby-on-rails-3 db2 rake jruby

使用以下声明:

rails generate scaffold Product title:string description:text image_url:string price:decimal

并稍微编辑生成的迁移我得到了以下迁移:

class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.string :title
      t.text :description
      t.string :image_url
      t.decimal :price, precision: 8, scale: 2

      t.timestamps
    end
  end
end

之后,我成功运行了以下命令:

rake db:migrate

并连接到我的数据库并检查表格是否已创建。然后,我跑

rake test

并且遇到了很多错误,我认为问题的根源是rails无法找到“产品”表:

 1) Error:
test_should_create_product(ProductsControllerTest):
ActiveRecord::JDBCError: Table products does not exist
    arjdbc/jdbc/RubyJdbcConnection.java:115:in `columns'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-jdbc-adapter-1.2.7/lib/arjdbc/db2/adapter.rb:514:in `columns'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:660:in `column_names'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:651:in `timestamp_column_names'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:585:in `table_rows'
    org/jruby/RubyHash.java:1257:in `each'
    org/jruby/RubyEnumerable.java:718:in `map'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:579:in `table_rows'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:494:in `create_fixtures'
    org/jruby/RubyArray.java:1613:in `each'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:492:in `create_fixtures'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:491:in `create_fixtures'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_adapter.rb:168:in `disable_referential_integrity'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:476:in `create_fixtures'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:895:in `load_fixtures'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:849:in `setup_fixtures'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:432:in `_run__858744690__setup__1004892786__callbacks'
    org/jruby/RubyBasicObject.java:1659:in `__send__'
    org/jruby/RubyKernel.java:2086:in `send'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `__run_callback'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:390:in `_run_setup_callbacks'
    org/jruby/RubyBasicObject.java:1659:in `__send__'
    org/jruby/RubyKernel.java:2086:in `send'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:81:in `run_callbacks'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activesupport-3.2.12/lib/active_support/testing/setup_and_teardown.rb:35:in `run'

据我所知,在进行测试时,我的“测试”数据库将被删除,并且“开发”数据库的迁移将适用于它。因为,我使用的是“IBM DB2 Express 10.1”数据库,我想我需要对负责测试的文件进行一些更改。

你能帮我解决这个问题吗?

下面,我展示了有关整个情况的更多信息:

  1. 使用“IBM DB2 Express”10.1版
  2. 使用jRuby 1.7.2
  3. 使用Rails版本3.2.12
  4. 使用Ubuntu 10.04 LTC
  5. 这就是我的“database.yml”文件的外观:
  6. development:
        adapter: jdbc
        driver: com.ibm.db2.jcc.DB2Driver
        url: jdbc:db2://localhost:50000/ddevelop
        host: localhost
        port: 50000
        database: ddevelop
        username: db2inst1
        password: pass
    
    test:
        adapter: jdbc
        driver: com.ibm.db2.jcc.DB2Driver
        url: jdbc:db2://localhost:50000/dtest
        host: localhost
        port: 50000
        database: dtest
        username: db2inst1
        password: pass
    

    注意:已经创建了“dtest”数据库。

1 个答案:

答案 0 :(得分:1)

测试数据库尚未更新。您需要在rake db:test:prepare之后运行rake db:migration。您现在可以运行它并重试。