I am currently on Chapter 6 on RoR tutorials (Hartl's) figuring out modeling users. And, I ran into a problem when I run the rake test - I constantly get an error message shown below:
/u$ bundle exec rake test
rake aborted!
test_should_get_new is already defined in UsersControllerTest
/Users/joebcvan/.rvm/gems/ruby-2.2.0@global/gems/activesupport-
4.2.0/lib/active_support/testing/declarative.rb:14:in `test'
.
.
.
/Users/joebcvan/.rvm/gems/ruby-2.2.0@global/gems/railties-
4.2.0/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/Users/joebcvan/.rvm/gems/ruby-2.2.0@global/gems/railties-
4.2.0/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
Tasks: TOP => test:run
(See full trace by running task with --trace)
So, I have taken a look at the user_Controller_test.rb file, and tried fidgeting here, but no luck. My user_controller_test.rb is shown below.
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
test "should get new" do
get :new
assert_response :success
end
end
I have tried deleting the four lines of code shown below
test "should get new" do
get :new
assert_response :success
end
and ran the rake test, and the error message now appears as below:
1) Error:
SiteLayoutTest#test_should_get_home:
SyntaxError: /Users/joebcvan/workspace/sample_app/app/models/user.rb:5: syntax
error, unexpected ':', expecting keyword_end
format: { with: VALID_EMAIL_REGEX}
^
Error:
SiteLayoutTest#test_should_get_home:
NoMethodError: undefined method `each' for nil:NilClass
2) Error:
SiteLayoutTest#test_should_get_help:
SyntaxError: /Users/joebcvan/workspace/sample_app/app/models/user.rb:5: syntax
error, unexpected ':', expecting keyword_end
format: { with: VALID_EMAIL_REGEX}
^
Error:
SiteLayoutTest#test_should_get_help:
NoMethodError: undefined method `each' for nil:NilClass
.
.
.
.
12) Error:
UserTest#test_name_should_not_be_too_long:
SyntaxError: /Users/joebcvan/workspace/sample_app/app/models/user.rb:5: syntax
error, unexpected ':', expecting keyword_end
format: { with: VALID_EMAIL_REGEX}
^
13) Error:
UserTest#test_should_be_valid:
SyntaxError: /Users/joebcvan/workspace/sample_app/app/models/user.rb:5: syntax
error, unexpected ':', expecting keyword_end
format: { with: VALID_EMAIL_REGEX}
^
13 runs, 0 assertions, 0 failures, 13 errors, 0 skips
And now I am stuck.. I am a beginner in RoR, and I feel helpless... I don't know how to figure out these error codes.
I will highly appreciate your expert advice please.
Thank you.
The errors are telling you a number of things, but it might all be related to the first error.
SyntaxError:
A syntax error is invalid code. The Ruby parser can't understand it, and the exception is kind enough to show you the offending line (as well as tell you the line number):
format: { with: VALID_EMAIL_REGEX}
^
The colon wasn't expected there. What I can't tell you is if you are missing an open bracket or not ({) on a preceding line. You'll have to double check your code around there.
Anything with nil
The line:
NoMethodError: undefined method `each' for nil:NilClass
Suggests that you are trying to call the each method on a variable that wasn't set. In this case it looks like you were trying to iterate through a collection. nil
is the default value for any variable until you set it with something else.
All your exceptions are variations of these two.
Tip: I would like to add some incident that might help others. While fixing after an error from test, be aware your next changed may not responded and keep display the same errors: Then you may need to kill existing process for that errors. Try
$ps -a
Looking for your app eg: sample_app
$23507 ttys004 0:00.36 spring server | sample_app | started 2 mins ago
Then kill
$kill -9 23507
And now run 'rails test' and continue to fix it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.