Bholzer
Bholzer

Reputation: 189

Why won't my rails spec run?

When I try to run my spec, I get an uninitialized constant error. My spec looks like this:

describe Facility do 
  it { should have_many(:units) }
  it { should have_many(:facilities_users) }
  it { should have_many(:administrators) }
  it { should have_many(:facility_employees) }
end

The error is: facility_spec.rb:1:in `<top (required)>': uninitialized constant Facility (NameError)

I certainly have a Facility model, so I'm not sure why this would happen.

Upvotes: 2

Views: 2474

Answers (4)

Prasanna Natarajan
Prasanna Natarajan

Reputation: 772

In the spec file, require the file where Facility class is defined.

Upvotes: 0

yagooar
yagooar

Reputation: 15979

If you are using the 'rspec-rails' gem, then run

rails g rspec:install

This will create the spec/spec_helper.rb file (you should edit it if you're not using ActiveRecord so it runs you spec setup correctly).

After that, ensure you are requiring the helper at the top of your spec files:

require 'spec_helper'

If this didn't work for you, there might be more issues like:

  1. You're trying to test a file under the lib/ directory. In this case, make sure this file is loaded with the environment (config/application.rb -> autoload_paths) or require it explicitly.
  2. The constant actually doesn't exist. It could be inside a namespace or just a typo.

Upvotes: 0

Danpe
Danpe

Reputation: 19057

You should try running rake spec instead of rspec spec. But both may work.

If not working try Try bundle exec rspec spec or bundle exec rake spec.

Source: When trying to run rspec I get uninitialized constant.

Upvotes: 2

Agis
Agis

Reputation: 33666

Add the following at the top of your file:

require 'spec_helper'

Upvotes: 0

Related Questions