Why am I getting Zeitwerk::NameError?

I’m trying to load a module (defined in lib/) in one of my test cases.

The error:

Error:
VendorApis::FooVendorTest#test_fetching_data_works_properly:
Zeitwerk::NameError: expected file /home/blaine/project/lib/vendor_apis/foo_vendor.rb to define constant VendorApis::FooVendor, but didn't
    app/models/inv_procure/vendor_api.rb:6:in `get_api_class_const'
    app/models/inv_procure/vendor_api_mapping.rb:42:in `api_instance'
    test/lib/vendor_apis/foo_vendor_test.rb:6:in `block in <class:FooVendorTest>'

This is how it’s being loaded:

  def get_api_class_const
    require "./lib/" + self.lib_path
    return Object.const_get(self.class_path)
  end

I think Zeitwerk (I don’t know much about this) is expecting a constant to be in the file, based on the file name, i.e. VendorApis::FooVendor. However, that is not the case. The file defines a class constant FooVendor::ApiConsumer. Is there a way around this?

By default, the lib directory is not in the autoload paths. Your application has to be adding it by hand.

The reason is that lib in the autoload paths is normally a bit tricky because the lib directory contains assorted stuff like Rake tasks, etc.

So, one question to ponder is: Does the application really need lib in the autoload paths?

If the answer is yes, do you need all of it or only part? Do you need autoload and reload? Because if you have something in the autoload paths, it is expected to follow certain conventions.

Once that understood, I will be able to help you in either case.