1

I'm running a simple test to list an objects attributes.

require 'spec_helper'

describe 'List Movies' do 
    it 'Lists all movies' do 

        movie1 = Movie.create(

            title: "Iron Man",
            rating: "PG-13",
            total_gross: 10000000

            )

        movie2 = Movie.create(

            title: "Super Man",
            rating: "PG",
            total_gross: 120000000

            )

        movie3 = Movie.create(

            title: "Spider Man",
            rating: "R-16",
            total_gross: 30000000

            )

        visit movies_url

        expect(page).to have_text("3 Movies")

        expect(page).to have_text(movie1.title)
        expect(page).to have_text(movie2.title)
        expect(page).to have_text(movie3.title)

        expect(page).to have_text(movie1.title)
        expect(page).to have_text(movie1.rating)
        expect(page).to have_text("10000000")


    end
end

It comes back to me with uninitialized constant Movie.

so i run a generator.

rails g model Movie title:string rating:string total_gross:decimal --no-test-framework.

I run the test again, rspec spec/features/list_movies_spec.rb.

Yet, still the same error, i've done this a hundred times before, why is it not recognizing the model after running the generator?.

1
  • 2
    Is this a new app using Rspec 3.0? If so, you should be requiring 'rails_helper' instead of 'spec_helper'. Commented Jul 12, 2014 at 3:52

1 Answer 1

2

As of Rspec 3.0, running rails g rspec:install creates a rails_helper.rb file now to be used instead of spec_helper.rb, so you should be requiring that instead at the top of your specs:

require 'rails_helper'

You'll see that the Rspec generator actually still created a spec_helper file, but that file does nothing; it's mostly commented out. rails_helper is where the Rails environment (which includes your models) is loaded.

The reasoning for this is that you may have specs that don't need to load the entire Rails stack, so those specs can continue to use the spec_helper file. There is an upgrade guide for moving from rspec-rails 2.x to 3.x

Sign up to request clarification or add additional context in comments.

1 Comment

Hey Dylan, do you know how to get rid of that massive stack dump that runs on the test?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.