【问题标题】:Rake Test Error - test_should_get new is already defined in UserControllerTestRake 测试错误 - test_should_get new 已在 UserControllerTest 中定义
【发布时间】:2015-02-03 12:13:48
【问题描述】:

我目前正在学习关于 RoR 教程(Hartl 的)的第 6 章,以了解建模用户。而且,我在运行 rake 测试时遇到了问题 - 我经常收到如下所示的错误消息:

/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)

所以,我查看了 user_Controller_test.rb 文件,并尝试在这里坐立不安,但没有运气。我的 user_controller_test.rb 如下所示。

require 'test_helper'

class UsersControllerTest < ActionController::TestCase

  test "should get new" do
    get :new
    assert_response :success
  end

end

我已经尝试删除下面显示的四行代码

test "should get new" do
    get :new
    assert_response :success
  end

并运行 rake 测试,现在错误消息如下所示:

  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

现在我被卡住了。。我是RoR的初学者,我感到无助...我不知道如何弄清楚这些错误代码。

非常感谢您的专家建议。

谢谢。

【问题讨论】:

  • 听起来好像有什么东西正在加载这个文件两次,或者你在另一个文件中定义了 UsersControllerTest

标签: ruby-on-rails ruby rake-test


【解决方案1】:

这些错误告诉你很多事情,但可能都与第一个错误有关。

语法错误:

语法错误是无效代码。 Ruby 解析器无法理解,并且异常好心地向您显示有问题的行(以及告诉您行号):

format: { with: VALID_EMAIL_REGEX}
      ^

那里没有预期的冒号。我不能告诉您的是,您是否在前一行缺少左括号 ({)。您必须仔细检查那里的代码。

nil

行:

NoMethodError: undefined method `each' for nil:NilClass

建议您尝试在未设置的变量上调用 each 方法。在这种情况下,您似乎正在尝试遍历集合。 nil 是任何变量的默认值,除非你用别的东西设置它。

您的所有例外都是这两者的变体。

【讨论】:

    【解决方案2】:

    提示: 我想补充一些可能对其他人有帮助的事件。 在测试错误后进行修复时,请注意您的下一个更改可能不会响应并继续显示相同的错误: 然后,您可能需要针对该错误终止现有进程。 试试

    $ps -a
    

    正在寻找您的应用,例如:sample_app

    $23507 ttys004    0:00.36 spring server | sample_app | started 2 mins ago
    

    然后杀死

    $kill -9 23507
    

    现在运行“rails test”并继续修复它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      • 2016-09-05
      • 2015-11-24
      相关资源
      最近更新 更多