fxn/zeitwerk

loader `for_gem` in rspec

faraaz-deepsource opened this issue · 4 comments

I am writing a gem and have it structured in the conventional way:

lib/
|- gem_name/
|   |- ...
|- gem_name.rb

Inside gem_name.rb I am using

loader = Zeitwerk::Loader.for_gem
loader.setup

This works fine when I build, install and use the gem. However, when I use it in a unit test in RSpec, it shows me uninitialized constant, even when I require_relative ../lib/gem_name.rb from spec/spec_helper.rb.

I don't understand, why isnt the GemName namespace in the LOAD_PATH

fxn commented

RSpec adds lib to $LOAD_PATH automatically, it should be enough to do

require "gem_name"

in spec/spec_helper.rb. Does that work in your project?

Yes it did work I was totally looking in the wrong place. Sorry to waste your time like this. Thank you for the help

@fxn okay I do have a working example at https://github.com/faraazahmad/zeitest
image

fxn commented

This project does not have RSpec set up as documented in the RSpec README, and because of that spec/spec_helper.rb is not being loaded.

Please have a look at the "Setup" section of https://github.com/rspec/rspec-metagem.