Rspec does not see my model class. uninitialized persistent error

I am writing Rspec tests for my models in a Ruby on Rails application. And I get this error when running the 'rspec spec'

command: /spec/models/client_spec.rb:4:in `<top (required)>': uninitialized constant Client (NameError) 

I am using Rails 4.0.0 and Ruby 2.0.0

Here is my client_spec.rb:

 require 'spec_helper' describe Client do it 'is invalid without first_name', :focus => true do client = Client.new client.should_not be_valid end end 

And gemfile:

 source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.0.0.rc1' # Use sqlite3 as the database for Active Record gem 'sqlite3' # Use SCSS for stylesheets gem 'sass-rails', '~> 4.0.0.rc1' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .js.coffee assets and views gem 'coffee-rails', '~> 4.0.0' # gem 'therubyracer', platforms: :ruby # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes following links in your web application faster. Read more: gem 'turbolinks' gem 'jbuilder', '~> 1.0.1' group :development do gem 'rspec-rails' end group :doc do # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', require: false end group :test do gem 'rspec-rails' gem 'factory_girl_rails' gem 'database_cleaner' end 

And finally .rb client (model and class ROR):

 class Client < ActiveRecord::Base has_many :cars has_many :orders has_one :client_status has_one :discount_plan, through: :client_status validates :email, format: { with: /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[az]{2,4})\z/, :message => "Only emails allowed", :multiline => true } validates :email, presence: true, if: "phone.nil?" #validates :phone, presence: true, if: "email.nil?" validates :last_name, :first_name, presence: true validates :last_name, :first_name, length: { minimum: 2, maximum: 500, wrong_length: "Invalid length", too_long: "%{count} characters is the maximum allowed", too_short: "must have at least %{count} characters" } end 

If it were useful for my spec_helper.rb file:

 # This file was generated by the `rspec --init` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # Require this file using `require "spec_helper"` to ensure that it is only # loaded once. # # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| config.treat_symbols_as_metadata_keys_with_true_values = true config.run_all_when_everything_filtered = true config.filter_run :focus # Run specs in random order to surface order dependencies. If you find an # order dependency and want to debug it, you can fix the order by providing # the seed, which is printed after each run. # --seed 1234 config.order = 'random' #config.use_transactional_fixtures = false config.before(:suite) do DatabaseCleaner.strategy = :transaction DatabaseCleaner.clean_with(:truncation) end config.before(:each) do DatabaseCleaner.start end config.after(:each) do DatabaseCleaner.clean end end 
+69
ruby-on-rails testing model rspec
Jul 6 '13 at 21:43
source share
7 answers

There are some important commands missing from your spec_helper file. In particular, it does not include configuration / environment and rspec-rails initialization.

You can add the following lines at the beginning 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 one generated for use with rspec-rails .

+74
Jul 07 '13 at 1:53 on
source share

On rails 4.x (rspec-rails 3.1.0) use

 require "rails_helper" # this 

not

 require "spec_helper" # not this 

in your spec files

+137
Jan 14 '15 at 1:17
source share

You can also add --require rails_helper to your .rspec file so it looks like this.

 --color --require spec_helper --require rails_helper 

After that, you will not need to request rails_helper in all of your specifications.

+10
Nov 22 '15 at
source share

I am using Rails 5.0.0.1.
This is how I solved this problem.

In your Gemfile add → gem 'rspec-rails', "> = 2.0.0.beta"

In this way,

 group :development, :test do gem 'rspec-rails', ">= 2.0.0.beta" end 

Reason: if rspec-rails is not added, and when you execute the rspec command, it will generate this error → "cannot load such a file - rails_helper"

Now run this command on the terminal.

install package

After the bundle command went well, run the rails generation. In this way,

rails creates rspec: install

Reason: this command will create a new .rspec (it will overwrite when prompted), spec / rails_helper.rb and spec / spec_helper.rb

Now, at this point, rspec should pretty much work properly.
However, if you encounter an error when it is not found in the model, for example. can't load such a 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: it seems that spec_helper is not loading the Rails environment. We demand it.

Hope this helps!

+5
Nov 29 '16 at 7:41
source share

Since this thread was created, the situation has changed a bit. I also experienced the error uninitialized constant ClassName (NameError) using Ruby 2.1, Rails 4.2, rspec-rails 3.3.

I solved my problems by reading the gspec-rails gem documentation:

https://github.com/rspec/rspec-rails#model-specs

where he confirms that Swards talks about the need for rails_helper, not spec_helper.

Also, my model specification is more like the one from 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 
+1
Nov 09 '15 at 15:46
source share

In your application, the "Factories" folder is defined

 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 
0
Apr 05 '18 at 6:08
source share

if other answers on this question do not work, try: - see if there is a typo in the file name or class name (they should match). In config/environment/test.rb find config.eager_load = false , set to true .

0
Dec 06 '18 at 7:56
source share



All Articles