測試/單位/ user_test.rb認爲:爲什麼這個非常基本的單元測試失敗(ruby 1.9.2p290,Rails 3.0.9)?
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
產生如下:上一個SQL
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
錯誤陳述意見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分支並重新開始,但是我真的很想知道是什麼讓我第一次觸動了我。請指出我是否應該查看日誌或其他信息來源以解決問題。
附錄(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
什麼是您的schema.rb文件?你可以發佈嗎? – jergason
你有沒有在'database.yml'文件中配置你的測試數據庫? –
現在將發佈架構。我沒有添加或刪除任何內容到默認數據庫YAML文件。 –