Forum: RSpec Tests for my gem cannot find classes in gems lib directory ..

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Anthony Broad-Crawford (Guest)
on 2009-04-22 08:55
(Received via mailing list)
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.

gem_name
  + lib
      +gem_name
        -foo.rb
  +spec
    - foo_spec.rb
  +tasks
    - spec.rake

my spec rake

require 'rubygems'
require 'spec'
require 'spec/rake/spectask'

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

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



and my spec

describe Foo do
end



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:in `load_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:in `load_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: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!
Matt W. (Guest)
on 2009-04-22 11:45
(Received via mailing list)
On 22 Apr 2009, at 01:43, Anthony Broad-Crawford wrote:

>     +gem_name
> 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.
http://beta.songkick.com
http://blog.mattwynne.net
This topic is locked and can not be replied to.