rspec/rspec-mocks — Issue #87: Error "evaluating nil.each" for any_instance.stub (outside of rspec).
Author: mattscilipoti
Created: 2011-10-21 17:48:07+00:00
State: open

I had a similar experience to what @lisad discusses in the comments of ticket #70. On rspec-rails 2.6.1, I received "undefined method:`playback!'". After upgrading to rspec-rails 2.7.0 (with rspec-mocks 2.7.0), I get the nil.each error when the stubbed method is called.

```
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each (NoMethodError)
./app/controllers/application_controller.rb:13:in `authenticate_user!'
```
Line 13 (above) is the call to `http_remote_user`.

I am attempting to mock our SSO authentication, which looks for the RemoteUser from Apache.

```cucumber
Given /^I am authenticated by AD as "([^"]+)"$/ do |username|
require 'rspec/mocks'
RSpec::Mocks::setup(ApplicationController)
ApplicationController.any_instance.stub(:http_remote_user).and_return(username)
end
```

In the application, I check to see if we can utilize SSO. I know this code probably isn't relevant, including (snipped) just in case. I tried making `http_remote_user` a public method.

```ruby
class ApplicationController < ActionController::Base
before_filter :authenticate_user!
...

protected
def authenticate_user!
if http_remote_user
# assign session
else
#login
end

private
# for SSO
def http_remote_user
# checks request for remote user
end

```

Any ideas?
I'll be looking into it, but thought it might be obvious to someone here.