I have a gem project with the following structure:
foo-bar
├── lib
│ └── foo
│ ├── bar
│ │ └── qux.rb
│ └── bar.rb
└── spec
├── spec_helper.rb
└── unit
├── baz_spec.rb
└── qux_spec.rb
In lib/foo
, bar.rb
defines a module Foo::Bar
, and inside that, a class Foo::Bar::Baz
. Inside lib/foo/bar
, qux.rb
defines a class Foo::Bar::Qux
.
spec_helper.rb
sets up RSpec and Simplecov, and finishes with require 'foo/bar'
. Both baz_spec.rb
and qux_spec.rb
start with require 'spec_helper'
.
baz_spec.rb
has the specs for Foo::Bar::Baz
, and it works fine. qux_spec.rb
, however, which has the specs for Foo::Bar::Qux
, fails with:
/Users/me/foo-bar/spec/unit/qux_spec.rb:6:in `<module:Bar>': uninitialized constant Foo::Bar::Qux (NameError)
from /Users/me/foo-bar/spec/unit/qux_spec.rb:4:in `<module:Foo>'
from /Users/me/foo-bar/spec/unit/qux_spec.rb:3:in `<top (required)>'
from /Users/me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1226:in `load'
...
(etc.)
I've verified that it's not just a typo by moving the code for Foo::Bar::Baz
out of lib/foo/bar.rb
and into its own file, lib/foo/bar/baz.rb
, after which baz_spec.rb
also stops working.
It also doesn't seem to make a difference whether I declare the class as
class Foo::Bar::Qux
...
or as
module Foo
module Bar
class Qux
...
I'm using Ruby 2.2.0 with RSpec 3.2.2 on Mac OS X Yosemite.
Clearly there's something wrong with my requires
, but as a Ruby novice I'm not seeing it. Any ideas?