为什么这个非常基本的单元测试失败了(ruby 1.9.2p290,Rails 3.0.9)?

时间:2011-09-05 16:20:33

标签: ruby-on-rails ruby ruby-on-rails-3 unit-testing

test / unit / user_test.rb成立:

require 'test_helper'

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

执行:

perrys-MacBook-Pro:iway perry_mac$ ruby -Itest test/unit/user_test.rb

产生以下内容:

Loaded suite test/unit/user_test
Started
E
Finished in 0.006410 seconds.

  1) Error:
test_the_truth(UserTest):
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: user_sessions: DELETE FROM "user_sessions" WHERE 1=1
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract_adapter.rb:207:in `rescue in log'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract_adapter.rb:199:in `log'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/connection_adapters/sqlite_adapter.rb:135:in `execute'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/database_statements.rb:288:in `update_sql'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/connection_adapters/sqlite_adapter.rb:139:in `update_sql'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/database_statements.rb:293:in `delete_sql'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/connection_adapters/sqlite_adapter.rb:145:in `delete_sql'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/database_statements.rb:54:in `delete'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/query_cache.rb:16:in `delete'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:569:in `delete_existing_fixtures'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:531:in `block (4 levels) in create_fixtures'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:531:in `each'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:531:in `block (3 levels) in create_fixtures'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:530:in `block (2 levels) in create_fixtures'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract_adapter.rb:109:in `disable_referential_integrity'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:521:in `block in create_fixtures'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/active_support/benchmarkable.rb:55:in `silence'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:520:in `create_fixtures'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:979:in `load_fixtures'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:944:in `setup_fixtures'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:415:in `_run_setup_callbacks'
    /Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:34:in `run'

1 tests, 0 assertions, 0 failures, 1 errors, 0 skips

错误语句对SQL DELETE查询发表评论,这看起来很奇怪。为什么它甚至在这里引用user_sessions?这个测试过去了。在一些数据库迁移(向现有表添加列)之后,它没有。有人能帮助我弥合我的理解差距吗?是否有一些我需要重装的神秘实体。我尝试了每一个:

rake db:test:clone  
rake db:test:clone_structure    
rake db:test:load   
rake db:test:prepare    
rake db:test:purge  

...但仍然看到相同的输出。我可以放弃当前的git分支并重新开始,但我真的很想知道是什么让我绊倒了。请说明我是否应该查看日志或其他信息来解决问题。

ADDENDUM(schema.rb):

ActiveRecord::Schema.define(:version => 20110901142600) do

  create_table "users", :force => true do |t|
    t.string   "username"
    t.string   "email"
    t.string   "crypted_password"
    t.string   "password_salt"
    t.string   "persistence_token"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.datetime "last_request_at"
    t.datetime "current_login_at"
    t.datetime "last_login_at"
    t.string   "current_login_ip"
    t.string   "last_login_ip"
  end

end

2 个答案:

答案 0 :(得分:2)

我认为关键是在堆栈跟踪的第10行:

/Users/perry_mac/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/fixtures.rb:569:in `delete_existing_fixtures'

注意fixtures.rbdelete_existing_fixtures。在运行测试套件之前,Rails会清除数据库中的所有fixture。您在user_sessions.yml中有test/fixtures个文件吗?

答案 1 :(得分:0)

我和大卫在一起;看起来你正在使用authlogic(或者类似的)并且没有完整的user_sessions设置生成了默认的模型fixture - 你是如何创建UserSession模型的?