Skip to content
This repository

undefined method `env' for nil:NilClass in RSpec 2.10 #534

Closed
sekrett opened this Issue May 04, 2012 · 28 comments

9 participants

Alexander Zubkov David Chelimsky José Valim Kevin Moore Ray Besiga Samer Masry 何旭东 Robert Reiz
Alexander Zubkov
sekrett commented May 04, 2012

Hello!

After upgrading to 2.10 in my Rails 3.2 application I get lots of errors:

1) PagesController as admin user new action should render new template
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `env' for nil:NilClass
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/devise-2.0.4/lib/devise/test_helpers.rb:33:in `warden'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-rails-2.10.0/lib/rspec/rails/adapters.rb:15:in `block (2 levels) in setup'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/example.rb:178:in `instance_eval'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/example.rb:178:in `instance_eval'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/hooks.rb:23:in `run'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/hooks.rb:63:in `block in run'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/hooks.rb:63:in `each'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/hooks.rb:63:in `run'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/hooks.rb:400:in `run_hook'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/example_group.rb:298:in `run_before_each_hooks'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/example.rb:239:in `run_before_each'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/example.rb:86:in `block in run'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/example.rb:195:in `with_around_each_hooks'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/example.rb:84:in `run'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/example_group.rb:353:in `block in run_examples'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/example_group.rb:349:in `map'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/example_group.rb:349:in `run_examples'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/example_group.rb:335:in `run'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/example_group.rb:336:in `block in run'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/example_group.rb:336:in `map'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/example_group.rb:336:in `run'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/command_line.rb:28:in `map'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/command_line.rb:28:in `block in run'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/reporter.rb:34:in `report'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/command_line.rb:25:in `run'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/runner.rb:69:in `run'
     # /Users/sekrett/.rvm/gems/ruby-1.9.3-p125-perf/gems/rspec-core-2.10.0/lib/rspec/core/runner.rb:10:in `block in autorun'

Probably it is related to Devise, since this error I see only for controllers. It never happened before. I can try to write a minimal application to reproduce the bug if needed.
For now I downgraded to 2.9 to make it work.

David Chelimsky
Owner

Thanks for reporting this here. I do have a guess as to the source of the problem, but can't really prove it w/o a minimal app to repro the bug. Thanks in advance for providing that.

The change that probably caused it is 47907b3, which reverses the order of any setup methods. That was done in a general way to address a specific problem, so if that is the source of the problem we'll need to prepend the specific setup method from Rails. I don't love that idea because it means if Rails changes the name of that setup method RSpec will experience the regression, but it might be a reasonable tradeoff.

Let me know when you have a sample app I can experiment with. Thanks!

Mark Cornick markcornick referenced this issue from a commit May 04, 2012
Commit has since been removed from the repository and is no longer available.
David Chelimsky
Owner

@josevalim FYI - pls read comments above and feel free to add commentary here.

José Valim

@dchelimsky maybe we can prepend just rails helpers? I think the issue is that Devise test helpers are also using setup and therefore being prepended by mistake.

I haven't taken a look at the source code but, if Rails has two callbacks (A and B) where B depends on A being executed first, wouldn't that be broken in rspec-rails if you prepend everything by default (the order would be reversed)?

David Chelimsky
Owner

@josevalim that's what I was suggesting in my previous comment. Specifically prepending only the setup_controller_request_and_response - but it means binding rspec-rails to a named method in Rails. I'm willing to do it as a temp solution but I don't like the dependency (as I'm sure you can imagine).

David Chelimsky
Owner

FYI - I'm going to go ahead and make that change and get a fix release out, but probably not until tomorrow.

José Valim

@dchelimsky instead of listing the method names, can't you list the class/module/self? It is more unlikely that the class/module name will change instead of the method name.

David Chelimsky
Owner

@josevalim self is going to be a subclass of ExampleGroup. We could inspect caller, but that feels just as icky to me as the incoming name. Maybe ickier. If I'm missing what you're suggesting, can you be more specific about which class/module/self you're talking about?

José Valim

Yeah, that's what I meant. :( So no other option then.

David Chelimsky
Owner

It's probably the right thing to do, but I'll be sad if a Rails release breaks it. Hopefully travis-ci will help me know if/when that happens in advance.

David Chelimsky dchelimsky closed this issue from a commit May 05, 2012
David Chelimsky Only prepend_before the Rails setup methods
- Fixes #534
- Unfortunately depends on names of methods that are internal to Rails
  and subject to change. Fingers crossed.
71006ce
David Chelimsky dchelimsky closed this in 71006ce May 04, 2012
David Chelimsky
Owner

@sekrett can you verify this fixes your problem? Once you do I'm going to release this over the weekend.

Alexander Zubkov
sekrett commented May 04, 2012

Yes, on master it works now. Thank you.

David Chelimsky
Owner

Just released rspec-rails-2.10.1 with this fix.

Kevin Moore
kevmoo commented May 05, 2012

Ditto for me. Thanks!

Ray Besiga

Just got rspec-rails-2.10.0 with the same error. If I edit my Gemfile.lock to rspec-rails-2.10.1 and bundle update, will that get rid of the error?

Alexander Zubkov
sekrett commented May 07, 2012

You'd better issue the command bundle update rspec-install to do that automatically.

Ray Besiga

Thanks. I did that but it did not fix my problem. Is it okay if I post the backtrace? It says something about 'block on autorun' every time I run Guard

David Chelimsky
Owner

@raybesiga you don't need to ask to post a backtrace. Please do.

Ray Besiga

Here it is:

C:\Sites\tasks>guard
←[0m
Guard here! It looks like your project has a Gemfile, yet you are running
guard outside of Bundler. If this is your intent, feel free to ignore this
message. Otherwise, consider using bundle exec guard to ensure your
dependencies are loaded correctly.
(You can run guard with --no-bundler-warning to get rid of this message.)
←[0m
←[0mGuard could not detect any of the supported notification libraries.←[0m
←[0mGuard is now watching at 'C:/Sites/tasks'←[0m
←[0mGuard::RSpec is running, with RSpec 2!←[0m
←[0m←[0mRunning all specs←[0m
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/test/unit/testcase.rb:9:in <class:Te
stCase>': uninitialized constant Test::Unit::TestCase::Assertions (NameError)
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/test/unit/testcase.rb:8:
in
module:Unit'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/test/unit/testcase.rb:4:
in <module:Test>'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/test/unit/testcase.rb:3:
in
'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/test/unit.rb:5:in <top
(required)>'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/test/unit/assertions.rb:
4:in
'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-rails-2.
10.1/lib/rspec/rails/adapters.rb:2:in <top (required)>'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-rails-2.
10.1/lib/rspec/rails.rb:10:in
'
from C:/Sites/tasks/spec/spec_helper.rb:4:in <top (required)>'
from C:/Sites/tasks/spec/controllers/tasks_controller_spec.rb:1:in
requ
ire'
from C:/Sites/tasks/spec/controllers/tasks_controller_spec.rb:1:in <top
(required)>'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
0.0/lib/rspec/core/configuration.rb:746:in
load'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
0.0/lib/rspec/core/configuration.rb:746:in block in load_spec_files'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
0.0/lib/rspec/core/configuration.rb:746:in
map'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
0.0/lib/rspec/core/configuration.rb:746:in load_spec_files'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
0.0/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
0.0/lib/rspec/core/runner.rb:69:in run'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
0.0/lib/rspec/core/runner.rb:10:in
block in autorun'

Samer Masry
smasry commented June 21, 2012

I just encountered the same issue. Had to change my before callback to a before(:each)

何旭东
chucai commented July 10, 2012

I have the same problem. but when I downgrade to 2.9.0 make it work.

David Chelimsky
Owner

@raybesiga use bundle exec guard as the output of guard tells you.

@smasry what callback?

@chucai the bug is in 2.10.0, but fixed in 2.10.1. Did you upgrade to 2.10.1 or 2.11.0?

Ray Besiga

Thanks! @dchelimsky

何旭东
chucai commented July 12, 2012

@dchelimsky Thanks!

Robert Reiz
reiz commented July 16, 2012

I am using Rails 3.2.6 and RSpec 2.11.0 and I have the same issue.

Robert Reiz
reiz commented July 16, 2012

I downgraded to RSpec 2.9.0 and still getting "undefined method `env' for nil:NilClass". Any ideas for a quick workaround? Or is it possible that it is not related to this issue?

Right now I have to disable SSL for the UI Tests to make the tests work. Would be cool if I don't have to work around RSpec. I appreciate any kind of feedback. Many thanks.

David Chelimsky
Owner

@reiz doubt that's the same issue. This particular issue is unique to rspec-rails-2.10.0. It was not present for 2.9 or 2.10.1. Please submit a separate issue with the relevant spec, code, and failure message.

Robert Reiz
reiz commented July 17, 2012

@dchelimsky OK. Will do. Many Thanks.

Future Googlers:
I had a similar issue with rspec-rails 2.10.1 and devise 2.1.1:
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method result' for #<NoMethodError: undefined methodfields' for nil:NilClass>
# ./config/initializers/activerecord_relation_ext.rb:6:in `select_ids'

It wasn't as obviously a devise compatibility issue, but I saw devise in the --backtrace and after reading through this issue I upgraded both gems and the error hasn't come up since! Note: The error was sporadic and usually cropped up more when running a full feature rather than a single scenario. The now-working gem combination is rspec-rails 2.14.0 and devise 3.0.0.

Jonathan Perkin jperkin referenced this issue from a commit in joyent/pkgsrc June 02, 2012
Update ruby-rspec-rails to 2.10.1.
### 2.10.1 / 2012-05-03
[full changelog](http://github.com/rspec/rspec-rails/compare/v2.10.0...v2.10.1)

Bug fixes

* fix regression introduced in 2.10.0 that broke integration with Devise
  (rspec/rspec-rails#534)

### 2.10.0 / 2012-05-03
[full changelog](http://github.com/rspec/rspec-rails/compare/v2.9.0...v2.10.0)

Bug fixes

* `render_views` called in a spec can now override the config setting. (martinsvalin)
* Fix `render_views` for anonymous controllers on 1.8.7. (hudge, mudge)
* Eliminate use of deprecated `process_view_paths`
* Fix false negatives when using `route_to` matcher with `should_not`
* `controller` is no longer nil in `config.before` hooks
* Change `request.path_parameters` keys to symbols to match real Rails
  environment (Nathan Broadbent)
* Silence deprecation warnings in pre-2.9 generated view specs (Jonathan del
  Strother)
5189558
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.