2012-12-03 51 views
1

嗨我想學習測試,但我的單元測試不起作用。所以我有一個用戶模型(和其他車型一樣好,但還沒有開始測試他們還):Rails:簡單的單元測試不在終端執行

class Users < ActiveRecord::Base 
    attr_accessible :age, :gender, :name 

    has_many :sent_messages, :class => "Messages", :foreign_key => 'sender_id' 
    has_many :received_messages, :class => "Messages", :foreign_key => 'receiver_id' 

    belongs_to :location 
    belongs_to :language 

    before_save { |user| if user.new_record? then create_remember_token end } 


    private 

     def create_remember_token 
      self.remember_token = SecureRandom.urlsafe_base64 
     end 

end 

和測試/單位/ users_test.rb

require 'test_helper' 

class UsersTest < ActiveSupport::TestCase 
    test "user is created" do 
    user = User.create!(:name => "Edmund") 
    assert_equal user, User.first 
    end 
end 

但是當我運行rake test:units我得到:

Edmunds-MacBook-Pro:langexchange edmundmai$ rake test:units 
NOTICE: CREATE TABLE will create implicit sequence "languages_id_seq" for serial column "languages.id" 
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index "languages_pkey" for table "languages" 
NOTICE: CREATE TABLE will create implicit sequence "locations_id_seq" for serial column "locations.id" 
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index "locations_pkey" for table "locations" 
NOTICE: CREATE TABLE will create implicit sequence "messages_id_seq" for serial column "messages.id" 
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index "messages_pkey" for table "messages" 
NOTICE: CREATE TABLE will create implicit sequence "users_id_seq" for serial column "users.id" 
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index "users_pkey" for table "users" 
/Users/edmundmai/.rvm/gems/[email protected]/gems/activesupport-3.2.8/lib/active_support/core_ext/hash/keys.rb:51:in `block in assert_valid_keys': Unknown key: class (ArgumentError) 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/activesupport-3.2.8/lib/active_support/core_ext/hash/keys.rb:50:in `each_key' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/activesupport-3.2.8/lib/active_support/core_ext/hash/keys.rb:50:in `assert_valid_keys' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/activerecord-3.2.8/lib/active_record/associations/builder/association.rb:33:in `validate_options' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/activerecord-3.2.8/lib/active_record/associations/builder/association.rb:24:in `build' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/activerecord-3.2.8/lib/active_record/autosave_association.rb:139:in `build' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/activerecord-3.2.8/lib/active_record/associations/builder/belongs_to.rb:14:in `build' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/activerecord-3.2.8/lib/active_record/associations/builder/association.rb:12:in `build' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/activerecord-3.2.8/lib/active_record/associations.rb:1428:in `belongs_to' 
    from /Users/edmundmai/Desktop/Class/Ruby/langexchange/app/models/messages.rb:4:in `<class:Messages>' 
    from /Users/edmundmai/Desktop/Class/Ruby/langexchange/app/models/messages.rb:1:in `<top (required)>' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `block in require' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependency' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:359:in `require_or_load' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:313:in `depend_on' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:225:in `require_dependency' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/railties-3.2.8/lib/rails/engine.rb:439:in `block (2 levels) in eager_load!' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/railties-3.2.8/lib/rails/engine.rb:438:in `each' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/railties-3.2.8/lib/rails/engine.rb:438:in `block in eager_load!' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/railties-3.2.8/lib/rails/engine.rb:436:in `each' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/railties-3.2.8/lib/rails/engine.rb:436:in `eager_load!' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/railties-3.2.8/lib/rails/application/finisher.rb:53:in `block in <module:Finisher>' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/railties-3.2.8/lib/rails/initializable.rb:30:in `instance_exec' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/railties-3.2.8/lib/rails/initializable.rb:30:in `run' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/railties-3.2.8/lib/rails/initializable.rb:55:in `block in run_initializers' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/railties-3.2.8/lib/rails/initializable.rb:54:in `each' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/railties-3.2.8/lib/rails/initializable.rb:54:in `run_initializers' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/railties-3.2.8/lib/rails/application.rb:136:in `initialize!' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/railties-3.2.8/lib/rails/railtie/configurable.rb:30:in `method_missing' 
    from /Users/edmundmai/Desktop/Class/Ruby/langexchange/config/environment.rb:5:in `<top (required)>' 
    from /Users/edmundmai/Desktop/Class/Ruby/langexchange/test/test_helper.rb:2:in `require' 
    from /Users/edmundmai/Desktop/Class/Ruby/langexchange/test/test_helper.rb:2:in `<top (required)>' 
    from /Users/edmundmai/Desktop/Class/Ruby/langexchange/test/unit/helpers/home_helper_test.rb:1:in `require' 
    from /Users/edmundmai/Desktop/Class/Ruby/langexchange/test/unit/helpers/home_helper_test.rb:1:in `<top (required)>' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/rake-10.0.2/lib/rake/rake_test_loader.rb:10:in `require' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/rake-10.0.2/lib/rake/rake_test_loader.rb:10:in `block (2 levels) in <main>' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/rake-10.0.2/lib/rake/rake_test_loader.rb:9:in `each' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/rake-10.0.2/lib/rake/rake_test_loader.rb:9:in `block in <main>' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/rake-10.0.2/lib/rake/rake_test_loader.rb:4:in `select' 
    from /Users/edmundmai/.rvm/gems/[email protected]/gems/rake-10.0.2/lib/rake/rake_test_loader.rb:4:in `<main>' 
rake aborted! 
Command failed with status (1): [ruby -I"lib:test" -I"/Users/edmundmai/.rvm/gems/[email protected]/gems/rake-10.0.2/lib" "/Users/edmundmai/.rvm/gems/[email protected]/gems/rake-10.0.2/lib/rake/rake_test_loader.rb" "test/unit/**/*_test.rb" ] 
Tasks: TOP => test:units 
(See full trace by running task with --trace) 

這怎麼可能?有人可以幫忙嗎?我沒有寫任何其他測試,這樣我不知道它是如何抓狂

UPDATE 消息模型:

class Messages < ActiveRecord::Base 
    attr_accessible :content, :read 

    belongs_to :sender, :class => "User", :foreign_key => 'sender_id' 
    belongs_to :receiver, :class => "User", :foreign_key => 'receiver_id' 
end 
+0

你運行'耙分貝:測試:prepare'? –

+0

嘿伊恩。不,我從來沒有見過。這是我在運行測試之前必須運行的標準命令嗎? – Edmund

+0

正在運行,只是給我提供了'NOTICE:'與您在上面看到的創建表。之後,我再次運行'rake test:units',沒有任何改變。錯誤仍然是一樣的 – Edmund

回答

5

在你的用戶模型使用

has_many :sent_messages, :class => "Messages", :foreign_key => 'sender_id' 
has_many :received_messages, :class => "Messages", :foreign_key => 'receiver_id' 

,你應該使用:

has_many :sent_messages, :class_name => "Messages", :foreign_key => 'sender_id' 
has_many :received_messages, :class_name => "Messages", :foreign_key => 'receiver_id' 

(切換:class:class_name


在協會有沒有這樣的東西:class選項。你想要的是:class_name選項。 (你將不得不改變以往任何時候都使用這種模式。)

http://guides.rubyonrails.org/association_basics.html#has_many-association-reference

+0

謝謝!通過將':class'替換爲':class_name'修復了'User'和'Message'模型。謝謝!! – Edmund

0

從這個堆棧跟蹤,你似乎有一個錯誤在你Messages模型。如果你糾正了這個錯誤,這應該正確運行。

+0

嘿維拉,你能告訴我哪一行幫你弄清楚它是'消息'嗎? – Edmund

+0

我翻遍了(並粘貼在問題中)我的消息模型...我沒有看到它的任何錯誤... – Edmund

+0

'from/Users/edmundmai/Desktop/Class/Ruby/langexchange/app/models/messages .rb:4:在''中看起來像這樣是錯誤:Unknown key:class(ArgumentError) – Magicmarkker