views:

788

answers:

2

I'm attempting to use rspec in a rails project I've just upgraded to rails 2.3.2. I've installed rspec 1.2.6 and rspec-rails 1.2.6 as plugins in the app.

My problem is the specs don't have access to my app classes or any of the rails standard libraries.

First I had to specify the model class I want to test by using the full path from RAILS_ROOT but now as it loads the class I get the following

/app/models/person.rb:1: uninitialized constant ActiveRecord (NameError)
        from ./spec/models/person_spec.rb:1:in `require'
    from ./spec/models/person_spec.rb:1
    from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:15:in `load'
    from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:15:in `load_files'
    from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:14:in `each'
    from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:14:in `load_files'
    from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/options.rb:99:in `run_examples'
    from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/command_line.rb:9:in `run'
    from /Users/law/Projects/roster/vendor/plugins/rspec/bin/spec:4
rake aborted!

I am launching rspec by calling rake spec from the root of the application.

Any ideas on what might be missing in this situation?

A: 

I havn't used spec, so this may not solve your problem, but if you're writing your own rake task and need your rails environment, you have to ask for it.

task(:task_name => :environment) do
    # Task Implementation Here
end
Mike
Cheers for the help. I'm not rolling my own rake tasks, but have had a look and its already running a pretty complicated dependency structure for the task in question. So I'm not sure I want to mess with it.
lyallward
+4  A: 

you need indeed include the spec_helper.rb in every spec file you write....

You can run individual specs that way:

$ spec specs/models/person_spec.rb

instead of always running the whole spec suite

jcfischer
That's the solution I worked out. It feels redundant so I guess I was hoping there was a better solution out there.
lyallward