I\'m writing tests on Rspec for my models in Ruby on Rails application. And I receive this error while starting \'rspec spec\'
command:
/spec/models/client_spec
Your spec_helper
file is missing some important commands. Specifically, it's not including config/environment and initializing rspec-rails
.
You can add the following lines to the start of your spec/spec_helper.rb
file
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
or you can just run
rails generate rspec:install
and overwrite your spec_helper
with one generated for use with rspec-rails
.
If other answers under this question don't work, try:
Other wise,
config/environment/test.rb
file,
see if there is config.eager_load = false
, set it to true
.You should check in the written order since you don't want to solve the issue with the typo laying there.
I'm using Rails 5.0.0.1.
Here's how I resolved this concern.
On your Gemfile, please add -> gem 'rspec-rails', ">= 2.0.0.beta"
Like so,
group :development, :test do
gem 'rspec-rails', ">= 2.0.0.beta"
end
Reason: if the rspec-rails is not added and when you execute the rspec command, it will generate this error -> "cannot load such file -- rails_helper"
Now, execute this command on the terminal.
bundle install
Once bundle command went good, execute the rails generate. Like so,
rails generate rspec:install
Reason: this command will create a new .rspec(hit overwrite when prompted), spec/rails_helper.rb and spec/spec_helper.rb
Now, at this point, rspec should pretty much run properly.
However, if you encounter an error where in the model is not found e.g. cannot load such file -- idea, try adding this on top of your spec/spec_helper.rb
require 'rubygems'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
Reason: seems that spec_helper is not loading the Rails environment. We're requiring it.
Hope this helps!
You might also like to add --require rails_helper
in your .rspec
file so that it looks like this.
--color
--require spec_helper
--require rails_helper
You won't need to require rails_helper in all your specs, after this.
Things have moved a bit since this thread has been created, I have experienced the uninitialized constant ClassName (NameError)
error too using Ruby 2.1, Rails 4.2, rspec-rails 3.3.
I have solved my problems reading the rspec-rails gem documentation :
https://github.com/rspec/rspec-rails#model-specs
where it confirms what Swards says about requiring "rails_helper" not "spec_helper" anymore.
Also my model specification looks more like the one from the gem docs
RSpec.describe Url, :type => :model do
it 'is invalid without first_name', :focus => true do
client = Client.new
client.should_not be_valid
end
end
Factories folder define in your app
FactoryBot.define do
factory :user_params , :class => 'User' do
username 'Alagesan'
password '$1234@..'
end
end
Your Controller RSpec file:
it 'valid params' do
post :register, params: {:user => user_params }
end