Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uninitialized constant RSpec for rspec > 3 #61

Closed
joonty opened this issue Aug 8, 2014 · 5 comments
Closed

uninitialized constant RSpec for rspec > 3 #61

joonty opened this issue Aug 8, 2014 · 5 comments

Comments

@joonty
Copy link
Contributor

joonty commented Aug 8, 2014

Hi,

I'm not sure whether this is a bug or a problem with my environment, but I'm consistently getting this error immediately on starting tork (using version 19.10.0):

/home/jon/projects/secretz/spec/spec_helper.rb:17:in `<top (required)>': uninitialized constant RSpec (NameError)

The spec_helper isn't anything special, just the one generated by rails (using rspec-rails version 3.0.3, the latest):

# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause this
# file to always be loaded, without a need to explicitly require it in any files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, make a
# separate helper file that requires this one and then use it only in the specs
# that actually need it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
  # ... etc

I did a little digging, and noted that earlier versions of rspec-rails explicitly put the following at the top of the spec helper:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# ... etc

Adding this back in to the spec helper seems to fix the problem, but I'm very aware that the rspec-rails team removed this for a reason. Do you have any thoughts on the best way to approach this? I.e. should this code be added to allow for use with tork, or is there something you can add to tork itself to automatically load the environment and rspec-rails?

Maybe a first action would be to add it to the documentation, in case other people are coming across it.

Hope this is helpful. Thanks

@joonty
Copy link
Contributor Author

joonty commented Aug 8, 2014

Actually, I've noticed that this doesn't fix it. The problem is that rspec-rails now creates a spec_helper and a rails_helper. The environment loading code is done at the top of the rails_helper, but it looks like tork requires the spec_helper. I think the rspec-rails team now expect people to load the rails_helper, which will in turn load everything needed.

So, I hacked around in tork to check for the rails_helper instead of spec_helper, and it almost worked. I had to change the first few lines of the rails_helper from:

ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'

to:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'spec_helper'

I.e. the spec_helper had to be loaded after the rest of the environment.

So it seems like checking for the rails_helper as a compatibility for rspec-rails 3+ might be a fix, but there's also the issue of the environment not being loaded before the spec_helper when using the rails_helper generated out of the box.

Is this making any sense?

Thanks for the great gem, I love it!

@sunaku
Copy link
Owner

sunaku commented Aug 8, 2014

Thanks for the positive feedback! 😺

Your spec/spec_helper.rb file is being loaded into tork-master(1) by the "spec" configuration helper. But rather than patching it, I think we should add a new "rspec-rails" configuration helper that detects if "rspec-rails" is installed and only then load the Rails environment. If we can get all this to run before the normal "spec" helper (perhaps by prefixing onto $TORK_CONFIGS rather than appending), then it ought to work. :neckbeard:

@joonty
Copy link
Contributor Author

joonty commented Sep 24, 2014

I was convinced that I'd replied to this. Turned out I hadn't. Sigh.

I think yours is a much better idea than mine. 👍

@sunaku
Copy link
Owner

sunaku commented Sep 24, 2014

Alright, is there as easy way to detect if "rspec-rails" is installed? 🍰

Can we test if a particular file exists or must we grep the spec_helper file?

sunaku pushed a commit that referenced this issue Oct 24, 2014
Fixes `uninitialized constant RSpec` for rspec > 3.

Closes issue #61.  See also issues #62 and #63.
@sunaku
Copy link
Owner

sunaku commented Oct 24, 2014

Merged your fixes in #62 and #63 and released as Tork 19.11.1, thanks! 👍

@sunaku sunaku closed this as completed Oct 24, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants