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
Related
I am trying to run the specs for a local clone of the activeadmin gems current master.
I am using ruby 2.2.2 and have bundled successfully.
When I try to run one of the specs like so:
bundle exec rspec spec/unit/filters/humanized_spec.rb
I am getting the following error:
The git source https://github.com/jruby/activerecord-jdbc-adapter is not yet checked out. Please run `bundle install` before trying to start your application
Coverage report generated for RSpec to /Users/aljoscha/gem/activeadmin/coverage. 0.0 / 0.0 LOC (100.0%) covered.
/Users/aljoscha/gem/activeadmin/spec/rails_helper.rb:13:in `require': cannot load such file -- active_record (LoadError)
from /Users/aljoscha/gem/activeadmin/spec/rails_helper.rb:13:in `<top (required)>'
from /Users/aljoscha/gem/activeadmin/spec/unit/filters/humanized_spec.rb:1:in `require'
from /Users/aljoscha/gem/activeadmin/spec/unit/filters/humanized_spec.rb:1:in `<top (required)>'
from /Users/aljoscha/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `load'
from /Users/aljoscha/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `block in load_spec_files'
from /Users/aljoscha/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `each'
from /Users/aljoscha/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `load_spec_files'
from /Users/aljoscha/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:100:in `setup'
from /Users/aljoscha/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:86:in `run'
from /Users/aljoscha/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:71:in `run'
from /Users/aljoscha/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:45:in `invoke'
from /Users/aljoscha/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.5.4/exe/rspec:4:in `<top (required)>'
from /Users/aljoscha/.rvm/gems/ruby-2.2.2/bin/rspec:22:in `load'
from /Users/aljoscha/.rvm/gems/ruby-2.2.2/bin/rspec:22:in `<main>'
from /Users/aljoscha/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval'
from /Users/aljoscha/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>'
which I cannot make sense of.
Especially since activerecord-jdbc-adapter is not even in the Gemfile.
What am I missing?
Try prepending your command with bundle exec:
bundle exec rspec spec/unit/filters/humanized_spec.rb
It looks like you have a local version discrepancy causing issues.
If not, did you follow all of the steps listed on their contributing readme?
If you are still stuck, you may be experiencing an issue with bundler itself. First, try updating bundler then reinstalling gems and attempt to run rspec. If that doesn't work you may need to delete the bundle cache folder and start over.
I'm going to re-answer to emphasize how to test a gem using appraisal as mentioned in CONTIBUTING:
bundle exec appraisal install
bundle exec appraisal rails_50 rspec spec/unit/filters/humanized_spec.rb
Thank you for your interest in Active Admin.
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
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.
I've just installed a fresh Ruby on Rails dev stack on my new Macbook, which runs Mac OS X 10.9 Mavericks.
Here is my stack:
Xcode command line tools
Homebrew
Rbenv
Bundler
Ruby 2.1.0
Rails 4.1.1
I ran rails new test_app to generate a first test app.
This command successfully completed, but since it, every Rails command I try in my CLI returns this kind of error message:
$ test_app > bin/rails server
/Users/me/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/client/rails.rb:27:in `load': no implicit conversion of nil into String (TypeError)
from /Users/me/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/client/rails.rb:27:in `call'
from /Users/me/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/client/command.rb:7:in `call'
from /Users/me/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/client.rb:26:in `run'
from /Users/me/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/spring-1.1.3/bin/spring:48:in `<top (required)>'
from /Users/me/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/binstub.rb:11:in `load'
from /Users/me/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/binstub.rb:11:in `<top (required)>'
from /Users/me/.rbenv/versions/2.1.0/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/me/.rbenv/versions/2.1.0/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/me/Documents/[PERSONNEL]/dev/test_app/bin/spring:16:in `<top (required)>'
from bin/rails:3:in `load'
from bin/rails:3:in `<main>'
Same error while trying to start a console:
$ test_app > bin/rails c
/Users/me/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `load': no implicit conversion of nil into String (TypeError)
from /Users/me/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `block in load'
from /Users/me/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in `load_dependency'
from /Users/me/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `load'
from /Users/me/.rbenv/versions/2.1.0/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/me/.rbenv/versions/2.1.0/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
The bin/rake are working partially (I can use bin/rake routes but migrations are not working).
My last try was to install XCode, but it didn't solve it.
UPDATE: the command bundle exec rake rails:update:bin as mentioned bellow solve the problem for project, but needed to be executed on every new problem.
The problem seems to be deeper.
OK I found solution.
I've cleaned my mac too. and I got same error.
rails_root/bin folder is not there?
Try below
bundle exec rake rails:update:bin
I have the same problem. Short exploration follows below.
First of all, I have [] in the root path for my application. The error during rails s command
psylone#wizard:~/ruby-rails[code]/context$ rails s
/home/psylone/.rvm/gems/ruby-2.2.0/gems/spring-1.2.0/lib/spring/client/rails.rb:30:in `load': no implicit conversion of nil into String (TypeError)
is in spring gem here: https://github.com/rails/spring/blob/master/lib/spring/client/rails.rb#L27
That's because if Dir.glob contains [] in the path argument the result will be an empty array. So it's necessary to escape [] in the path argument for Dir.glob method. I think about something like this:
# Instead of line 27 in spring/client/rails.rb
require 'shellwords'
path = Shellwords.escape(Spring.application_root_path) << "/{bin,script}/rails"
load path
After this fix I've found rails s command works fine. But rails c command still causes an error. Probably the answer has the same nature.
So, the simplest way to avoid this error - rename the root path for your application (without [] characters).
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.