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

230 views
Skip to first unread message

Samantha John

unread,
Feb 20, 2011, 6:12:48 PM2/20/11
to rspec...@rubyforge.org
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!

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Samantha John

unread,
Feb 20, 2011, 7:07:59 PM2/20/11
to rspec...@rubyforge.org
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"

Justin Ko

unread,
Feb 20, 2011, 7:35:10 PM2/20/11
to rspec...@rubyforge.org

On Feb 20, 5:07 pm, Samantha John <li...@ruby-forum.com> wrote:
> 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"
>
> --

> Posted viahttp://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-us...@rubyforge.orghttp://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.

David Chelimsky

unread,
Feb 20, 2011, 7:01:44 PM2/20/11
to rspec...@rubyforge.org
On Feb 20, 2011, at 8:12 PM, Samantha John wrote:

> I am new to rspec.

Welcome!

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

Samantha John

unread,
Feb 21, 2011, 1:29:54 PM2/21/11
to rspec...@rubyforge.org
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

Reply all
Reply to author
Forward
0 new messages