【问题标题】:Rails strange error => IndexError: string not matchedRails 奇怪的错误 => IndexError: 字符串不匹配
【发布时间】:2011-02-04 20:05:07
【问题描述】:

我收到一个非常奇怪的消息错误。我认为它不是来自 Ruby,而是来自 unix 系统。

所以,我有以下测试文件:

require File.dirname(__FILE__) + '/../test_helper' 

class CatTest < ActiveSupport::TestCase
    def test_truth
    assert true
   end
end

因此,Fixtures 目录中的 YAML 文件没有任何内容。

当我使用命令运行上面的测试时:

$ ruby ./test/unit/cat_test.rb

我得到了非常奇怪的结果:

Loaded suite ./test/unit/cat_test
Started
E
Finished in 0.011252 seconds.

  1) Error:
test_truth(CatTest):
IndexError: string not matched


1 tests, 0 assertions, 0 failures, 1 errors

找不到错误的意思

IndexError: string not matched

但最奇怪的是,昨天,居然成功了!

非常感谢您的帮助。

问候

(我在 Ubuntu 9.04 下工作)

【问题讨论】:

    标签: ruby-on-rails unit-testing


    【解决方案1】:

    例如,当您尝试错误地将字符串变量作为 Hash 访问时,就会发生这种情况。

    s = "a string"
    s["position"] = "an other string"
    
    IndexError: string not matched
        from (irb):5:in `[]='
        from (irb):5
    

    Additional Information

    【讨论】:

    • 在我的情况下,我试图访问我认为是 JSON 对象的序列化 json(一个字符串)
    【解决方案2】:

    正如 Simone 指出的那样,当你认为你有一个哈希但实际上它是一个字符串时,就会导致错误。它是一个真正的 Ruby 异常。由于我的代码中的拼写错误,我遇到了异常。在您的情况下,我不确定原因,但这可能与 Ruby / Rails 版本的组合有关。

    您可以尝试对 Ruby 或 Rails 进行升级/降级,看看是否能解决问题。

    对于多个 Ruby 版本,我强烈推荐“RVM”:http://rvm.beginrescueend.com/

    【讨论】:

      猜你喜欢
      • 2016-11-20
      • 2018-06-13
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-23
      相关资源
      最近更新 更多