简体   繁体   中英

ruby module NameError: uninitialized constant error

I am trying to create a gem, but when trying to load a module in test I get the following error. I used " Configurable Ruby gems: Custom error messages and testing " to set environment variables from users and that's where most of the code is from.

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

but as per the folder structure and everything this should be Msg91sms::Errors::Configuration . I put only one here even though all tests are failing due to improper module loading.

The gem with this error can be found here: https://github.com/flyingboy007/msg91sms/tree/development

bundle exec rspec will throw all errors.

It should be something with naming or improper loading. But I can't figure out.

After following the answer by @sergio, I am now getting this error:

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

Could someone tell me what am doing wrong here?

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

Use fully qualified name to help ruby lookup the class.

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

The folder structure is like Errors::Configuration but the error is showing like Configuration::Errors..Dont know why..

It's trying to find Errors::Configuration within Msg91sms::Configuration (the current scope at that point). But since there's no Msg91sms::Configuration::Errors , it fails with that message.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM