Getting errors when using rspec - ruby-on-rails

I'm doing a tutorial on rspec and keep getting errors whenever I try to perform rake tests or run rspec manually through the terminal. Here's what I get:
mes-mbp:00_hello Me$ ls
hello.rb hello_spec.rb index.html
mes-mbp:00_hello Me$ rspec 00_hello/hello_spec.rb
/Users/Me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- spec_helper (LoadError)
from /Users/Me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1181:in `block in requires='
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1181:in `each'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1181:in `requires='
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration_options.rb:110:in `block in process_options_into'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration_options.rb:109:in `each'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration_options.rb:109:in `process_options_into'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration_options.rb:22:in `configure'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:96:in `setup'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:85:in `run'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:70:in `run'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:38:in `invoke'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/exe/rspec:4:in `<top (required)>'
from /Users/Me/.rvm/gems/ruby-2.2.0/bin/rspec:23:in `load'
from /Users/Me/.rvm/gems/ruby-2.2.0/bin/rspec:23:in `<main>'
from /Users/Me/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `eval'
from /Users/Me/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `<main>'
Can anyone tell me what I'm doing wrong?
Edit: More information on what I'm doing as requested:
I'm following along with this tutorial. In the code above I'm trying to run tests on the hello.rb file that are in the hello_spec.rb file which has the 'require hello' code in it.

You are giving RSpec the wrong path to your spec file.
The current working directory is 00_hello.
mes-mbp:00_hello Me$ rspec 00_hello/hello_spec.rb
Will cause RSpec to look for 00_hello\00_hello\hello_spec.rb.
If you have run rspec --init may have a .rspec file which will require spec_helper.rb if it contains the line:
--require spec_helper
The most common practice when writing specs is to place specs in a directory named spec. That way you can run all the project specs by
$ rspec specs
Added:
This is a very common structure for gems and other ruby projects:
lib/
hello.rb
spec/
spec_helper.rb
hello_spec.rb
Its so common that RSpec will add the /lib and /spec directory to the load path automatically.
So you can do:
# spec/hello_spec.rb
require 'spec_helper'
require 'lib/hello' # instead of a relative path! Wehoo.
RSpec.describe Hello do
# ...
end

Related

LearnRubyTheHardWay Ex#46 - rake aborted, you should require 'minitest/autorun'

I'm on Example 46 of Learn Ruby The Hard Way - Creating a Project Skeleton
Link: http://learnrubythehardway.org/book/ex46.html
So I created all the directories which show up as the following:
skeleton/
NAME.gemspec
Rakefile
data
ext/
tests/
bin/
NAME
doc/
lib/
NAME
NAME.rb
lib/NAME
tests/
test_NAME.rb
I'm in the Skeleton directory in Powershell.
When I run rake test as the tutorial says to I get an error.
Here's the result from Powershell:
PS C:\ruby\learn\projects\skeleton> rake test
C:/RailsInstaller/Ruby2.1.0/bin/ruby.exe -I"lib;tests" -I"C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4
.2/lib" "C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb" "tests/test_NAME
.rb"
Warning: you should require 'minitest/autorun' instead.
Warning: or add 'gem "minitest"' before 'require "minitest/autorun"'
From:
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/test/unit.rb:1:in `<top (required)>'
C:/ruby/learn/projects/skeleton/tests/test_NAME.rb:2:in `<top (required)>'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb:15:in `block in <main>'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb:4:in `select'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb:4:in `<main>'
MiniTest::Unit::TestCase is now Minitest::Test. From C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/test/unit/testcase.rb:8:
in `<module:Unit>'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/test/unit.rb:676:in `<class:Runner>': undefined method `_run_suite' for class
`Test::Unit::Runner' (NameError)
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/test/unit.rb:261:in `<module:Unit>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/test/unit.rb:15:in `<module:Test>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/test/unit.rb:7:in `<top (required)>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from C:/ruby/learn/projects/skeleton/tests/test_NAME.rb:2:in `<top (required)>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb:15:in `block
in <main>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb:4:in `select'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb:4:in `<main>'
rake aborted!
Command failed with status (1): [ruby -I"lib;tests" -I"C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/
lib" "C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb" "tests/test_NAME.rb
" ]
Tasks: TOP => test
(See full trace by running task with --trace)
The book says I should get the following response:
Loaded suite tests/test_NAME
Started
.
Finished in 0.000226 seconds.
1 tests, 1 assertions, 0 failures, 0 errors
But instead I get an error. I've search for fixes and using gem install minitest but keep in mind I don't have a gemfile and I don't know if I should for this type of project. If I needed one I assume the tutorial would say so. So what do I do to fix this issue?
You don't need a Gemfile. While you are at skeleton directory, just do:
gem install 'test-unit'
and then from the same directory:
rake test
All tests will pass.
P.S. I just reproduced the whole thing locally following your tutorial and got the error message as yours. Then, I installed the test-unit gem and after that the rake test is working without any error.

Rspec: Running Rspec v 2.99 in Rails but have v 3.0 installed

So the Gemfile.lock of my Rails Framework sets my Rspec to v 2.99 while my computer runs 3.0.0. When I run my rspec test, I get:
/Users/MrChan/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1051:in `load': cannot load such file -- /Users/MrChan/Desktop/matt/week_2/db-drill-ar-student-schema-challenge/source/ar-student-schema/db/spec/student_spec.rb (LoadError)
from /Users/MrChan/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1051:in `block in load_spec_files'
from /Users/MrChan/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1051:in `each'
from /Users/MrChan/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1051:in `load_spec_files'
from /Users/MrChan/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:97:in `setup'
from /Users/MrChan/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:85:in `run'
from /Users/MrChan/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:70:in `run'
from /Users/MrChan/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:38:in `invoke'
from /Users/MrChan/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-core-3.0.0/exe/rspec:4:in `<top (required)>'
from /Users/MrChan/.rbenv/versions/2.1.1/bin/rspec:23:in `load'
from /Users/MrChan/.rbenv/versions/2.1.1/bin/rspec:23:in `<main>'
I know that this may be a stretch, but does anyone have any ideas? I've taken a look at my rspec tests and tried changing be_true to be_truthy in line with Rspec v 3.0, but I'm still getting an error...
Your error doesn't seem to be an Rspec version error. The first line says "cannot load such file (LoadError)" - that indicates it's trying to load a spec file, but can't. The file path is /Users/MrChan/Desktop/matt/week_2/db-drill-ar-student-schema-challenge/source/ar-student-schema/db/spec/student_spec.rb, so you should check:
Does the file exist?
Can you open it?
A common source of this error is trying to run rspec within a project subdirectory. When you run rspec Rspec looks for a spec/ directory. If you're in a subdirectory - maybe db in this example - then Rspec may look in the wrong place. Given that it seems like you're working through this code, I'm pretty sure that's where the error arose.

"Unable to autoload constant" using rspec but not rails

I've this file which I would like to test.
app/workers/station/http.rb
module Worker
module Station
class HTTP
# ...
end
end
end
This is my spec file.
spec/workers/station/http_spec.rb
describe Worker::Station::HTTP do
it "should do something" do
end
end
The problem now is that I'm getting the following error when running the spec file using rspec.
rspec spec/workers/station/http_spec.rb
/Users/linus/.rvm/gems/ruby-2.0.0-p247#global/gems/activesupport-4.0.4/lib/active_support/dependencies.rb:464:in `load_missing_constant': Unable to autoload constant Station::HTTP, expected app/workers/station/http.rb to define it (LoadError)
from /Users/linus/.rvm/gems/ruby-2.0.0-p247#global/gems/activesupport-4.0.4/lib/active_support/dependencies.rb:184:in `const_missing'
from spec/workers/station/http_spec.rb:3:in `<top (required)>'
from /Users/linus/.rvm/gems/ruby-2.0.0-p247#global/gems/activesupport-4.0.4/lib/active_support/dependencies.rb:223:in `load'
from /Users/linus/.rvm/gems/ruby-2.0.0-p247#global/gems/activesupport-4.0.4/lib/active_support/dependencies.rb:223:in `block in load'
from /Users/linus/.rvm/gems/ruby-2.0.0-p247#global/gems/activesupport-4.0.4/lib/active_support/dependencies.rb:214:in `load_dependency'
from /Users/linus/.rvm/gems/ruby-2.0.0-p247#global/gems/activesupport-4.0.4/lib/active_support/dependencies.rb:223:in `load'
from /Users/linus/.rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from /Users/linus/.rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `each'
from /Users/linus/.rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from /Users/linus/.rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:22:in `run'
from /Users/linus/.rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
from /Users/linus/.rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'
from /Users/linus/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/linus/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
The strange thing is that everyting works in the console.
$ rails c
[1] pry(main)> Worker::Station::HTTP
=> Worker::Station::HTTP
Why is this happening using rspec and not in rails and how would I fix it?
I'm using
rails (4.0.4)
rspec (2.14.1)
As jfornoff suggests you could add a require statement to the spec with a statement something like the following:
require "app/workers/station/http"
But if you are using the Spring Rails application preloader and the above doesn't resolve the problem, you could also check to see if Spring needs to be restarted. You can test running the spec without using Spring as follows:
bundle exec rspec spec/workers/station/http_spec.rb
... or ...
spring stop # or bin/spring stop
rspec spec/workers/station/http_spec.rb
The app/workers path is not being autoloaded by rspec because it is not standard rails layout, you can add an autoload line to your spec_helper or require the file in the spec directly!
Note: If solutions above/below do not work for you.
In Rails 4.1 and 4.2 I got similar error while trying to run the following command
$ spring rspec spec/classes/email_parser/email_provider_spec.rb:163
even tried
$ rspec spec/classes/email_parser/email_provider_spec.rb:163
Unable to autoload constant Account::Onboarding
Called from /Users/john/.rbenv/versions/2.3.8/lib/ruby/gems/2.3.0/gems/activesupport-4.2.11.3/lib/active_support/dependencies.rb:238:in `block in load_dependency'
/Users/john/.rbenv/versions/2.3.8/lib/ruby/gems/2.3.0/gems/activesupport-4.2.11.3/lib/active_support/dependencies.rb:495:in `load_missing_constant': Unable to autoload constant Account::Onboarding, expected /Users/john/Projects/Core/app/models/account/onboarding.rb to define it (LoadError)
from /Users/john/.rbenv/versions/2.3.8/lib/ruby/gems/2.3.0/gems/activesupport-4.2.11.3/lib/active_support/dependencies.rb:184:in `const_missing'
from /Users/john/Projects/Core/app/models/account.rb:9:in `<class:Account>'
from /Users/john/Projects/Core/app/models/account.rb:3:in `<top (required)>'
Cause
Turns out, I recently restarted my Mac and forgot to start the PG server. Not finding the PG server to respond to port 5432, the app seems to be crashing with logs that do not make any sense.
Solution that worked for me
I started the PG server.
# For Mac
$ pg_ctl -D /usr/local/var/postgresql#11 start
# For Linux
$ sudo service postgresql start

Ruby On Rails - Rspec issue

Trying to run
$ bundle exec rspec spec/requests/static_pages_spec.rb
from chapter 3.2.1 of this tutorial
However I'm getting the following error before the test can even run:
/Users/alexrahr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load': cannot load such file -- /Users/alexrahr/Apps/ruby.railstutorial.org/rails_projects/sample_app/app/spec/requests/static_pages_spec.rb (LoadError)
from /Users/alexrahr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /Users/alexrahr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
from /Users/alexrahr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /Users/alexrahr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
from /Users/alexrahr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
from /Users/alexrahr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
Any ideas what it might be? I'm guessing it's that I don't have the RSpec gem installed?
As far as I know the spec/ directory nest is based on the type of spec:
for model specs: spec/models/user_spec.rb
for controller specs: spec/controllers/users_controller_spec.rb
Also make sure you ran the rake rspec:install command after bundle
bundle && rake rspec:install
The error clearly states that the file you are trying to run is not found in the directory you say it is in.

Cannot generate rails command: bundle exec rspec spec/requests

I'm trying to start a second rails project, and for my firstone, rspec worked fine. I copied over the Gemfile to have the same gems, and I have the spec/requests directory in my project file. However, I get this error when I try to execute
C:\Sites\rails_projects\friendapp\app\views\static_pages>bundle exec rspec spec/
requests
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec
/core/configuration.rb:780:in load': cannot load such file -- C:/Sites/rails_pr
ojects/friendapp/app/views/static_pages/spec/requests (LoadError)
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
1.1/lib/rspec/core/configuration.rb:780:inblock in load_spec_files'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
1.1/lib/rspec/core/configuration.rb:780:in map'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
1.1/lib/rspec/core/configuration.rb:780:inload_spec_files'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
1.1/lib/rspec/core/command_line.rb:22:in run'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
1.1/lib/rspec/core/runner.rb:69:inrun'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
1.1/lib/rspec/core/runner.rb:8:in `block in autorun'
Any clues? Thanks!
It looks like you are not executing bundle exec from the root of the app, but rather from app\views\static_pages:
C:\Sites\rails_projects\friendapp\app\views\static_pages>
The error message says that the file does not exist from that location (static_pages):
C:/Sites/rails_projects/friendapp/app/views/static_pages/spec/requests
Change to the root dir and try again
C:\Sites\rails_projects\friendapp\>bundle exec rspec spec/requests

Resources