Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec tests load error: cannot load such file -- spec_helper (LoadError)

I'm following a chapter on RSpec test, but I keep getting a load error.

It tells me first to create a new branch, then to make two new directories: Models and Spec. In these directories I am supposed to make two files; models/entry.rb and spec/entry_spec.rb. In the spec/entry_spec.rb, I'm supposed to enter this code:

require_relative '../models/entry'


RSpec.describe Entry do

end

Then in my models/entry.rb I am supposed to put this code:

class Entry

end

I am then supposed to run this terminal command:

$ rspec spec/entry_spec.rb

And get this result:

No examples found.

Finished in 0.00015 seconds (files took 0.1186 seconds to load)
0 examples, 0 failures

But I'm always getting this load error though and I have no idea what it means

/Users/ethanfranson/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- spec_helper (LoadError)
    from /Users/ethanfranson/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/configuration.rb:1295:in `block in requires='
    from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/configuration.rb:1295:in `each'
    from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/configuration.rb:1295:in `requires='
    from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/configuration_options.rb:109:in `block in process_options_into'
    from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/configuration_options.rb:108:in `each'
    from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/configuration_options.rb:108:in `process_options_into'
    from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/configuration_options.rb:21:in `configure'
    from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/runner.rb:105:in `setup'
    from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/runner.rb:92:in `run'
    from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/runner.rb:78:in `run'
    from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/runner.rb:45:in `invoke'
    from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/exe/rspec:4:in `<top (required)>'
    from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `load'
    from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `<main>'
    from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
    from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'

If any one would take the time to help me through What I may be doing wrong, I would really appreciate it. Sorry for the long winded question!

like image 798
Ethan Franson Avatar asked Feb 05 '16 06:02

Ethan Franson


2 Answers

Double check that you are running rspec spec/entry_spec.rb from the root of app directory and not from within the spec directory.

Rspec adds the spec to the load path automatically. If you're already in the spec directory, rspec is going to add spec/spec to the load path instead.

like image 96
harshs08 Avatar answered Sep 23 '22 16:09

harshs08


First make sure you have installed rspec-rails correctly:

rails g rspec:install

You don't need to require you model files or any other files in /app for that matter. They are autoloaded when you require rails_helper.

# spec/models/entry_spec.rb
require 'rails_helper'
RSpec.describe Entry do

end

RSpec can generate this spec for you with:

rails generate rspec:model entry

You should also setup the generators in your config/application.rb:

# ...
config.generators do |g|
  g.test_framework :rspec
end

That way when you run the regular rails generators RSpec such as rails g model entry it will create spec files for you as well.

Note that older versions of rspec-rails you would require 'spec_helper', while in the current version you would use require 'rails_helper' since it splits the general RSpec config and the rails setup into two seperate files.

like image 45
max Avatar answered Sep 25 '22 16:09

max