我是铁杆新手,我不知道如何继续。我正在尝试完成“RailsSpace”一书,但在测试部分遇到错误。 (对于任何可能拥有该书的人,第119页)
Rails 3.2.8
ruby 1.9.3p194
在控制台中运行时:
@error_messages = ActiveRecord::Errors.default_error_messages; 0
我收到此错误:
NameError:未初始化的常量ActiveRecord ::错误
运行rake test:units
我明白了:
DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /Users/home/rails_projects/retrospection/Rakefile:7)
DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /Users/home/rails_projects/retrospection/config/environment.rb:5)
Run options:
# Running tests:
EEEEE
Finished tests in 0.108070s, 46.2663 tests/s, 0.0000 assertions/s.
1) Error:
test_screen_name_maximum_length(UserTest):
NameError: uninitialized constant ActiveRecord::Errors
/Users/home/rails_projects/retrospection/test/unit/user_test.rb:7:in `setup'
2) Error:
test_screen_name_minimum_length(UserTest):
NameError: uninitialized constant ActiveRecord::Errors
/Users/home/rails_projects/retrospection/test/unit/user_test.rb:7:in `setup'
3) Error:
test_uniqueness_of_screen_name_and_email(UserTest):
NameError: uninitialized constant ActiveRecord::Errors
/Users/home/rails_projects/retrospection/test/unit/user_test.rb:7:in `setup'
4) Error:
test_user_invalidity(UserTest):
NameError: uninitialized constant ActiveRecord::Errors
/Users/home/rails_projects/retrospection/test/unit/user_test.rb:7:in `setup'
5) Error:
test_user_validity(UserTest):
NameError: uninitialized constant ActiveRecord::Errors
/Users/home/rails_projects/retrospection/test/unit/user_test.rb:7:in `setup'
5 tests, 0 assertions, 0 failures, 5 errors, 0 skips
rake aborted!
Command failed with status (5): [/Users/home/.rvm/rubies/ruby-1.9.3-p194/bi...]
Tasks: TOP => test:units
我的 user_test.rb 文件如下所示:
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@error_messages = ActiveRecord::Errors.default_error_messages
@valid_user = users(:valid_user)
@invalid_user = users(:invalid_user)
end
#This user should be valid by construction.
def test_user_validity
assert @valid_user.valid?
end
#This user should be invalid by construction.
def test_user_invalidity
assert !@invalid_user.valid?
attributes = [:screen_name, :email, :password]
attributes.each do |attribute|
#assert @invalid_user.errors.invalid?(attribute) OLD CODE DOES NOT WORK IN RAILS3
assert @invalid_user.invalid?(attribute)
end
end
def test_uniqueness_of_screen_name_and_email
user_repeat = User.new( :screen_name => @valid_user.screen_name,
:email => @valid_user.email,
:password => @valid_user.password)
assert !user_repeat.valid?
assert_equal @error_messages[:taken], user_repeat.errors.on(:screen_name)
assert_equal @error_messages[:taken], user_repeat.errors.on(:email)
end
#MAKE SURE THE SCREEN NAME CAN'T BE TO SHORT
def test_screen_name_minimum_length
user = @valid_user
min_length = User::SCREEN_NAME_MIN_LENGTH
#Screen name is too short.
user.screen_name = "a" * (min_length - 1)
assert !user.valid?, "#{user.screen_name} should raise a minimum length error"
#Format the error message based on minimum length.
correct_error_message = sprintf(@error_messages[:too_short], min_length)
assert_equal correct_error_message, user.errors.on(:screen_name)
#Screen name is minimum length.
user.screen_name = "a" * min_length
assert user.valid?, "#{user.screen_name} should be just long enough to pass"
end
#MAKE SURE THE SCREEN NAME CAN'T BE TOO LONG
def test_screen_name_maximum_length
user = @valid_user
max_length = User::SCREEN_NAME_MAX_LENGTH
# Screen name is too long.
user.screen_name = "a" * (min_length + 1)
assert !user.valid?, "#{user.screen_name} should raise a maximum length error"
#Format the error message based on maximum length.
correct_error_message = sprintf(@error_messages[:too_long], max_length)
assert_equal correct_error_message, user.errors.on(:screen_name)
#Screen name is maximumlength.
user.screen_name = "a" * max_length
assert user.valid?, "#{user.screen_name} should be just short enough to pass"
end
end
根据错误消息“/ Users / home / rails_projects / retrospection / test / unit / user_test.rb:7:in”setup“”导致问题的一行是
@error_messages = ActiveRecord::Errors.default_error_messages