Rails:简单的单元测试不在终端中执行

时间:2012-12-03 20:26:42

标签: ruby-on-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

和test / unit / 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/ruby-1.9.3-p194@rails3tutorial2ndEd/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/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/core_ext/hash/keys.rb:50:in `each_key'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/core_ext/hash/keys.rb:50:in `assert_valid_keys'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/associations/builder/association.rb:33:in `validate_options'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/associations/builder/association.rb:24:in `build'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/autosave_association.rb:139:in `build'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/associations/builder/belongs_to.rb:14:in `build'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/associations/builder/association.rb:12:in `build'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/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/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `block in require'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:359:in `require_or_load'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:313:in `depend_on'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:225:in `require_dependency'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/engine.rb:439:in `block (2 levels) in eager_load!'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/engine.rb:438:in `each'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/engine.rb:438:in `block in eager_load!'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/engine.rb:436:in `each'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/engine.rb:436:in `eager_load!'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/application/finisher.rb:53:in `block in <module:Finisher>'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/initializable.rb:30:in `run'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/initializable.rb:54:in `each'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/application.rb:136:in `initialize!'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/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/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/rake-10.0.2/lib/rake/rake_test_loader.rb:10:in `require'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/rake-10.0.2/lib/rake/rake_test_loader.rb:10:in `block (2 levels) in <main>'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/rake-10.0.2/lib/rake/rake_test_loader.rb:9:in `each'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/rake-10.0.2/lib/rake/rake_test_loader.rb:9:in `block in <main>'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/rake-10.0.2/lib/rake/rake_test_loader.rb:4:in `select'
    from /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/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/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/rake-10.0.2/lib" "/Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

从该堆栈跟踪中,您的Messages模型中似乎有错误。如果您更正了该错误,则应该正确运行。