Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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)
like image 685
Linus Oleander Avatar asked Apr 14 '14 14:04

Linus Oleander


2 Answers

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
like image 53
danielmbarlow Avatar answered Nov 03 '22 16:11

danielmbarlow


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!

like image 22
jfornoff Avatar answered Nov 03 '22 16:11

jfornoff