[rspec-users] rspec not loading rails files- uninitialized constant error

David Chelimsky dchelimsky at gmail.com
Sun Feb 20 19:01:44 EST 2011


On Feb 20, 2011, at 8:12 PM, Samantha John wrote:

> I am new to rspec.

Welcome!

> I followed a few tutorials and set it up to test my
> existing rails project. I ran the rspec generators and created
> spec/user_spec.rb to test my user.rb model.
> 
> The user.rb file starts with:
> 
> class User < ActiveRecord::Base
> 
> 
> The user_spec.rb file starts with:
> 
> describe User do
> 
> When I run the test (using rake spec, bundle exec autotest,  rspec
> spec/user_spec.rb )
> I get the error:
> 
> `const_missing': uninitialized constant User (NameError)
> 
> within the following trace:
> 
> loading autotest/rspec2
> bundle exec /Users/Sam/.rvm/rubies/ruby-1.8.7-p330/bin/ruby -S
> /Users/Sam/.rvm/gems/ruby-1.8.7-p330/bundler/gems/rspec-core-011b1ce34016/bin/rspec
> --tty
> '/Users/Sam/Documents/Development/Friend-Mapper/friend_mapper_rails/spec/user_spec.rb'
> /Users/Sam/.rvm/gems/ruby-1.8.7-p330/bundler/gems/rspec-core-011b1ce34016/lib/rspec/core/backward_compatibility.rb:20:in
> `const_missing': uninitialized constant User (NameError)
>  from
> /Users/Sam/Documents/Development/Friend-Mapper/friend_mapper_rails/spec/user_spec.rb:1
>  from
> /Users/Sam/.rvm/gems/ruby-1.8.7-p330/bundler/gems/rspec-core-011b1ce34016/lib/rspec/core/configuration.rb:386:in
> `load'
>  from
> /Users/Sam/.rvm/gems/ruby-1.8.7-p330/bundler/gems/rspec-core-011b1ce34016/lib/rspec/core/configuration.rb:386:in
> `load_spec_files'
>  from
> /Users/Sam/.rvm/gems/ruby-1.8.7-p330/bundler/gems/rspec-core-011b1ce34016/lib/rspec/core/configuration.rb:386:in
> `map'
>  from
> /Users/Sam/.rvm/gems/ruby-1.8.7-p330/bundler/gems/rspec-core-011b1ce34016/lib/rspec/core/configuration.rb:386:in
> `load_spec_files'
>  from
> /Users/Sam/.rvm/gems/ruby-1.8.7-p330/bundler/gems/rspec-core-011b1ce34016/lib/rspec/core/command_line.rb:18:in
> `run'
>  from
> /Users/Sam/.rvm/gems/ruby-1.8.7-p330/bundler/gems/rspec-core-011b1ce34016/lib/rspec/core/runner.rb:55:in
> `run_in_process'
>  from
> /Users/Sam/.rvm/gems/ruby-1.8.7-p330/bundler/gems/rspec-core-011b1ce34016/lib/rspec/core/runner.rb:46:in
> `run'
>  from
> /Users/Sam/.rvm/gems/ruby-1.8.7-p330/bundler/gems/rspec-core-011b1ce34016/lib/rspec/core/runner.rb:10:in
> `autorun'
>  from
> /Users/Sam/.rvm/gems/ruby-1.8.7-p330/bundler/gems/rspec-core-011b1ce34016/bin/rspec:4
> 
> I tried a sample app (http://relishapp.com/rspec/file/twominutetutorial)
> which worked fine. I suspect that the issue is that rspec is not loading
> up the files from my rails project. Which configurations do I need to
> fix for this to work?

When you run the rspec:install generator, it creates a spec/spec_helper.rb file that loads the Rails environment. This file needs to be required from your user_spec.rb file, which should be spec/models/user_spec.rb, not spec/user_spec.rb.

HTH,
David


More information about the rspec-users mailing list