Tests for my gem cannot find classes in gems lib directory

I am writing a gem and using RSpec to drive my development. However,
whenever I describe a class within the gems lib I get an uninitialized
constant error. I am placing my folder structure, spec.rake and first
spec below. I feel I must be missing something obvious.
Additionally, I did output the value of f in lib inclusion code and it
is traversing the lib folder correctly and I can instantiate the class
in my rake.spec. Thanks for your time.


  • lib
    • foo_spec.rb
    • spec.rake

my spec rake

require ‘rubygems’
require ‘spec’
require ‘spec/rake/spectask’

Dir[File.expand_path(“lib/**/*.rb”)].each do |f|
require f

Spec::Rake::SpecTask.new do |t|
t.spec_files = FileList[‘spec/*_spec.rb’]

and my spec

describe Foo do

The error I get

./spec/Foo_spec.rb:1: uninitialized constant Foo (NameError)
from /Library/Ruby/Gems/1.8/gems/rspec-1.2.4/lib/spec/runner/
example_group_runner.rb:15:in load' from /Library/Ruby/Gems/1.8/gems/rspec-1.2.4/lib/spec/runner/ example_group_runner.rb:15:inload_files’
from /Library/Ruby/Gems/1.8/gems/rspec-1.2.4/lib/spec/runner/
example_group_runner.rb:14:in each' from /Library/Ruby/Gems/1.8/gems/rspec-1.2.4/lib/spec/runner/ example_group_runner.rb:14:inload_files’
from /Library/Ruby/Gems/1.8/gems/rspec-1.2.4/lib/spec/runner/
options.rb:99:in run_examples' from /Library/Ruby/Gems/1.8/gems/rspec-1.2.4/lib/spec/runner/ command_line.rb:9:inrun’
from /Library/Ruby/Gems/1.8/gems/rspec-1.2.4/bin/spec:4
rake aborted!
Command /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/
ruby -I"/Library/Ruby/Gems/1.8/gems/rspec-1.2.4/lib" “/Library/Ruby/
Gems/1.8/gems/rspec-1.2.4/bin/spec” “spec/foo_spec.rb” failed

Thanks again!

On 22 Apr 2009, at 01:43, Anthony Broad-Crawford wrote:


require ‘spec/rake/spectask’

from /Library/Ruby/Gems/1.8/gems/rspec-1.2.4/lib/spec/runner/
command_line.rb:9:in `run’
from /Library/Ruby/Gems/1.8/gems/rspec-1.2.4/bin/spec:4
rake aborted!
Command /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/
bin/ruby -I"/Library/Ruby/Gems/1.8/gems/rspec-1.2.4/lib" “/Library/
Ruby/Gems/1.8/gems/rspec-1.2.4/bin/spec” “spec/foo_spec.rb” failed

Thanks again!

You need to make sure that the foo_spec.rb requires the code in lib.
The usual way to do this is to have a file ‘spec_helper’ in the root
of the spec folder which requires the files in lib, then require
spec_helper from foo_spec. That means that if you need to require
anything else (e.g. mocking libraries) for all your spec files, you
can just put it in spec_helper.

Take a look at the specs for Cucumber, or practially any project on
github that has a ‘spec’ folder in it’s root, for an example.

Matt W.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs