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

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

Closed
sekrett opened this issue May 4, 2012 · 28 comments
Closed

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

sekrett opened this issue May 4, 2012 · 28 comments
Assignees
Milestone

Comments

@sekrett
Copy link

sekrett commented May 4, 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.

@dchelimsky
Copy link
Contributor

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!

@dchelimsky
Copy link
Contributor

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

@josevalim
Copy link
Contributor

@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)?

@dchelimsky
Copy link
Contributor

@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).

@dchelimsky
Copy link
Contributor

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

@ghost ghost assigned dchelimsky May 4, 2012
@josevalim
Copy link
Contributor

@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.

@dchelimsky
Copy link
Contributor

@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?

@josevalim
Copy link
Contributor

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

@dchelimsky
Copy link
Contributor

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.

@dchelimsky
Copy link
Contributor

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

@sekrett
Copy link
Author

sekrett commented May 5, 2012

Yes, on master it works now. Thank you.

@dchelimsky
Copy link
Contributor

Just released rspec-rails-2.10.1 with this fix.

@kevmoo
Copy link

kevmoo commented May 5, 2012

Ditto for me. Thanks!

@raybesiga
Copy link

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?

@sekrett
Copy link
Author

sekrett commented May 7, 2012

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

@raybesiga
Copy link

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

@dchelimsky
Copy link
Contributor

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

@raybesiga
Copy link

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: inmodule: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<top (required)>'
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<top (required)>'
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<top (required)>'
from C:/Sites/tasks/spec/spec_helper.rb:4:in <top (required)>' from C:/Sites/tasks/spec/controllers/tasks_controller_spec.rb:1:inrequ
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:inload'
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:inmap'
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:inrun'
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:inblock in autorun'

@smasry
Copy link

smasry commented Jun 22, 2012

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

@chucai
Copy link

chucai commented Jul 11, 2012

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

@dchelimsky
Copy link
Contributor

@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?

@raybesiga
Copy link

Thanks! @dchelimsky

@chucai
Copy link

chucai commented Jul 13, 2012

@dchelimsky Thanks!

@reiz
Copy link

reiz commented Jul 16, 2012

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

@reiz
Copy link

reiz commented Jul 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.

@dchelimsky
Copy link
Contributor

@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.

@reiz
Copy link

reiz commented Jul 17, 2012

@dchelimsky OK. Will do. Many Thanks.

@ronwsmith
Copy link

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.

jperkin pushed a commit to TritonDataCenter/pkgsrc-legacy that referenced this issue Dec 9, 2013
### 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)
jsonn pushed a commit to jsonn/pkgsrc that referenced this issue Oct 11, 2014
### 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants