Search code examples
ruby-on-railsrubyrspecrspec-rails

How to make a class visible for RSpec test file?


I have model Player, which lies in app/models:

class Player < ApplicationRecord
  ...
end

I'm using gem rspec-rails and I have file with a test that determines if player is successfully created, which lies in test/models:

require 'rspec'

require_relative '../../app/models/player.rb'

RSpec.describe 'Player' do           
  it 'saves new player' do
    expect(Player.new.save).to eq true
  end
end

I'm getting error NameError: uninitialized constant ApplicationRecord. This is probably because test classes don't see model classes. How to make them visible and avoid error, making possible to test creation of new player?


Solution

  • Add require 'rails_helper' to your spec at the top of the file. You can remove all other "requires".

    Docs: https://relishapp.com/rspec/rspec-rails/v/4-0/docs/model-specs