3

I understand that, this error is common but somehow I am not able to figure out the problem, seems like its path related. I followed this question also but no luck so far.

Gemfile

group :development, :test do
  gem 'faker'
  gem 'rspec-rails', '~> 3.7'
  gem 'factory_bot_rails', '~> 4.0'
  gem 'shoulda-matchers', '~> 3.1'    
end

spec/factories/user.rb

FactoryBot.define do
  factory :user do
    name "Andy"
  end
end

spec/models/user_spec.rb

require 'rails_helper'

RSpec.describe User, type: :model do

it "should be invalid without phone number" do
    user = build(:user)
    user.phone = nil
    expect(user).not_to be_valid
  end
end

spec/support/factory_bot.rb

RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end

.rspec

--require spec_helper
--color

rails_helper.rb

require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'support/factory_bot'
...
...

I am running bundle exec rspec spec/models/user_spec.rb

Things I tried:

  1. Added require 'support/factory_bot' in rails_helper.rb below require 'rspec/rails' line.
  2. rails generate rspec:install (executed successfully)
  3. rspec --init (executed successfully)
  4. rake db:test:prepare (executed successfully)
  5. Changed require 'rails_helper' to require 'spec_helper' in user_spec.rb but got error NameError: uninitialized constant User after run.

Here is the full trace:

An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: require 'rspec/rails'
TypeError:
  wrong argument type Class (expected Module)
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/abstract_controller/helpers.rb:181:in `include'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/abstract_controller/helpers.rb:181:in `block in add_template_helper'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/abstract_controller/helpers.rb:181:in `module_eval'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/abstract_controller/helpers.rb:181:in `add_template_helper'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/abstract_controller/helpers.rb:110:in `block in helper'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/abstract_controller/helpers.rb:109:in `each'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/abstract_controller/helpers.rb:109:in `helper'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/action_controller/railties/helpers.rb:17:in `inherited'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionview-5.0.6/lib/action_view/test_case.rb:11:in `<class:TestCase>'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionview-5.0.6/lib/action_view/test_case.rb:10:in `<module:ActionView>'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionview-5.0.6/lib/action_view/test_case.rb:8:in `<top (required)>'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `block in require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:259:in `load_dependency'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `block in require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:259:in `load_dependency'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `block in require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:259:in `load_dependency'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `block in require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:259:in `load_dependency'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `require'
# ./spec/rails_helper.rb:7:in `<top (required)>'
# ./spec/models/user_spec.rb:1:in `require'
# ./spec/models/user_spec.rb:1:in `<top (required)>'
No examples found.


Finished in 0.00789 seconds (files took 2.75 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
9
  • 1
    Please show the contents of your rails_helper file. Commented Mar 5, 2018 at 0:09
  • Also, the full stack trace would be helpful. Commented Mar 6, 2018 at 11:28
  • @moveson Added rails_helper.rb Commented Mar 7, 2018 at 19:09
  • What line does the error relate to? Can you show the stack trace? Commented Mar 7, 2018 at 23:08
  • 1
    @Imran In my case I created an helper module as class instead of module. Commented Jul 24, 2018 at 11:47

1 Answer 1

1

I got it working.

Everything was at the place except for one thing. Replaced module with class. I had a lib and a helper module which was causing the error. Just replaced module with class now it running like butter.

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

1 Comment

The only thing that could help us developers is to show which file failed to be loaded. In my case, I removed all the files from the project and started adding them one by one. Eventually, I've found the guilty files.

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.