ruby模块NameError:未初始化的常量错误

时间:2017-06-05 18:16:20

标签: ruby-on-rails ruby rspec

我正在尝试创建一个gem,但是当尝试在测试中加载模块时,我得到以下错误。我使用“Configurable Ruby gems: Custom error messages and testing”来设置用户的环境变量,这是大多数代码来自的地方。

  1) Msg91sms::Configuration with configuration block returns the correct authkey
     Failure/Error: raise Errors::Configuration, "Msg94 auth key missing!" unless @authkey

     NameError:
       uninitialized constant Msg91sms::Configuration::Errors
     # ./lib/msg91sms/configuration.rb:10:in `authkey'
     # ./spec/msg91sms/configuration_spec.rb:7:in `block (3 levels) in <top (required)>'

但是根据文件夹结构,这应该是Msg91sms::Errors::Configuration。即使由于模块加载不当导致所有测试都失败,我也只放了一个。

有错误的宝石可以在这里找到:https://github.com/flyingboy007/msg91sms/tree/development

bundle exec rspec会抛出所有错误。

它应该是命名或不正确加载的东西。但我无法弄明白。

在听完@sergio的回答之后,我现在收到这个错误:

  1) Msg91sms::Configuration with configuration block returns the correct authkey
     Failure/Error: raise ::Msg91sms::Errors::Configuration, "Msg91 auth key missing!" unless @authkey

     NameError:
       uninitialized constant Msg91sms::Errors
     # ./lib/msg91sms/configuration.rb:10:in `authkey'
     # ./spec/msg91sms/configuration_spec.rb:7:in `block (3 levels) in <top (required)>'

有人能告诉我这里有什么问题吗?

1 个答案:

答案 0 :(得分:2)

insn_const_string

使用完全限定名称来帮助ruby查找该类。

raise Errors::Configuration, "Msg94 auth key missing!" unless @authkey
  

文件夹结构类似于Errors :: Configuration,但错误显示为Configuration :: Errors ..不知道为什么..

它正试图在raise ::Msg91sms::Errors::Configuration, "Msg94 auth key missing!" unless @authkey 内找到Errors::Configuration(此时的当前范围)。但由于没有Msg91sms::Configuration,因此该消息失败。