Rspec not loading rails files- uninitialized constant error

I am new to rspec. 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?

Thanks!

Additional info:

Rails 3.0.3
Ruby 1.8.7

Gemfile:
gem “rspec-rails”, “>= 2.0.0.beta.10”, :git =>
“git://github.com/rspec/rspec-rails.git”
gem “rspec”, “>= 2.0.0.beta.10”, :git =>
“git://github.com/rspec/rspec.git”
gem “rspec-core”, “>= 2.0.0.beta.10”, :git =>
“git://github.com/rspec/rspec-core.git”
gem “rspec-expectations”, “>= 2.0.0.beta.10”, :git =>
“git://github.com/rspec/rspec-expectations.git”
gem “rspec-mocks”, “>= 2.0.0.beta.10”, :git =>
“git://github.com/rspec/rspec-mocks.git”

On Feb 20, 5:07pm, Samantha J. [email protected] wrote:

gem “rspec-core”, “>= 2.0.0.beta.10”, :git =>
[email protected]://rubyforge.org/mailman/listinfo/rspec-users
Hi Samantha, since you’re using Rails, you’ll need to run the “rspec
rails installer”. Take a look at the “Configure” section on this page:
http://relishapp.com/rspec/rspec-rails

Let us know if that works.

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

I am new to rspec.

Welcome!

loading autotest/rspec2
`load’

/Users/Sam/.rvm/gems/ruby-1.8.7-p330/bundler/gems/rspec-core-011b1ce34016/lib/rspec/core/command_line.rb:18:in

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

Thanks David, this was exactly what I needed.
Per your advice I have moved user_spec.rb to the model, I was
experimenting with moving it around to see if that would change
anything.
Thanks again, rspec is a really nice piece of software.

Sam