digitig
digitig

Reputation: 2130

RSpec can't see Rails models

I have a RSpec specification:

require 'spec_helper'
describe 'session_project' do
  before(:each) do
    @user = User.create(
        username: 'user',
        password: 'test',
        password_confirmation: 'test',
        email: '[email protected]'
    )
    @project = Project.create(name: 'Project 1', active: true, user_id: @user.id)
  end
  context 'when I am on the project page for Project 1' do
    it 'knows the current project is Project 1' do
      pending
    end
  end
end
# and so on.

User and Project are models of course - I want to test that the current project is being saved to and cleared from the session at the right times. When I run RSpec, it complains:

Failure/Error: @user = User.create(
NameError:
uninitialized constant User

If I cut-and-paste the User.create into a rails console it creates the user just fine.

Previous people who have asked about similar problems just needed to add require 'spec_helper', but I've already done that. So how do I make the Rails models available to RSpec?

Upvotes: 1

Views: 124

Answers (1)

Dave Newton
Dave Newton

Reputation: 160191

Under RSpec 3.x you need the rails_helper if you want Rails classes loaded up.

https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#default-helper-files

Upvotes: 1

Related Questions