Skip to content

Cannot stub view methods in before blocks (undefined method `view_context' for nil:NilClass) #833

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

Merged
merged 5 commits into from
Oct 14, 2013

Conversation

JonRowe
Copy link
Member

@JonRowe JonRowe commented Oct 9, 2013

After upgrading rspec to 2.14 we started to get the errors:

  1) campaigns/new 
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `view_context' for nil:NilClass
     # ./spec/support/sign_in_utils.rb:8:in `block (2 levels) in <top (required)>'

The reason seem to be that it is not possible to stub view methods from before blocks.

This is complete support file:

# spec/support/sign_in_utils.rb

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller

  config.before(:each, type: :view) do
    # RSpec doesn't expose the helper methods to the views
    # so just stubbing it.
    # Need to re-stub if we actually care about it in the specs
    view.stub(current_company: nil, current_user: nil)
    view.stub(current_currency: stub_model(Currency))
    view.stub(current_portal: Portals::NoPortal.new)
  end
end

@JonRowe
Copy link
Member

JonRowe commented Oct 3, 2013

Can you do me a favour and verify that the function your looking for exists in vanilla TestUnit/MiniTest?

/cc @alindeman

@dnagir
Copy link
Author

dnagir commented Oct 3, 2013

I'm not using minitest.
But whether it exists there it not this issue did not occure with rspec
2.13. Only in 2.14. So it definitely has something to do with rspec-rails.
On Oct 3, 2013 9:08 PM, "Jon Rowe" notifications@github.com wrote:

Can you do me a favour and verify that the function your looking for
exists in vanilla TestUnit/MiniTest?

/cc @alindeman https://github.com/alindeman


Reply to this email directly or view it on GitHubhttps://github.com//issues/833#issuecomment-25611689
.

@JonRowe
Copy link
Member

JonRowe commented Oct 3, 2013

rspec-rails wraps the Rails test helpers for RSpec. If you can replicate this in MiniTest it has nothing to do with RSpec, so by proving the functionality you desire exists in MiniTest, you prove theres a bug in RSpec.

@dnagir
Copy link
Author

dnagir commented Oct 3, 2013

Okay. I'll try tomorrow...

But the ONLY difference between working app and not is the version of
rspec-rails.
Nothing else changed whatsoever.
So how possibly can there be be an issue with anything other than rspec?
On Oct 3, 2013 9:59 PM, "Jon Rowe" notifications@github.com wrote:

rspec-rails wraps the Rails test helpers for RSpec. If you can replicate
this in MiniTest it has nothing to do with RSpec, so by proving the
functionality you desire exists in MiniTest, you prove theres a bug in
RSpec.


Reply to this email directly or view it on GitHubhttps://github.com//issues/833#issuecomment-25614538
.

@JonRowe
Copy link
Member

JonRowe commented Oct 4, 2013

It's a series of interconnected parts, it helps us understand why something might not work anymore

@dnagir
Copy link
Author

dnagir commented Oct 4, 2013

Ok. No worries.
I'll get back on this a little later with a bit more info. Can't do it
now-ish. Sorry for the delay.

On 4 October 2013 14:36, Jon Rowe notifications@github.com wrote:

It's a series of interconnected parts, it helps us understand why
something might not work anymore


Reply to this email directly or view it on GitHubhttps://github.com//issues/833#issuecomment-25676115
.

@dnagir
Copy link
Author

dnagir commented Oct 9, 2013

A bit more info on this issue:

The minimal rails example was created with rails new issue833 -O -S -T and is at https://github.com/dnagir/issue833

The relevant commit that reproduces is dnagir/issue833@b408cd8

For full repro just clone the repro, bundle install and run the specs.

The problem is that the before hook in the block RSpec.configure raises accessing the view variable.
But this doesn't happen outside of the RSpec.configure (eg within the example group, you'll can see it by uncommenting the support file)

The full backtrace is:

> bundle exec rspec --backtrace spec/views/registrations.html.erb_spec.rb 
FF

Failures:

  1) registrations/new rspec doesn't provide helper methods from ApplicationController so we stub while configuring 
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `view_context' for nil:NilClass
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-4.0.0/lib/action_view/test_case.rb:202:in `view'
     # ./spec/support/sign_in.rb:3:in `block (2 levels) in <top (required)>'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:237:in `instance_eval'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:237:in `instance_eval'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:21:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:85:in `block in run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:85:in `each'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:85:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:446:in `run_hook'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:345:in `run_before_each_hooks'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:300:in `run_before_each'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:113:in `block in run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:179:in `call'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:179:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:106:in `block (2 levels) in run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:108:in `call'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:108:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:446:in `run_hook'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:340:in `run_around_each_hooks'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:111:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:390:in `block in run_examples'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:386:in `map'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:386:in `run_examples'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:371:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:372:in `block in run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:372:in `map'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:372:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/command_line.rb:28:in `map'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/command_line.rb:28:in `block in run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/reporter.rb:58:in `report'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/command_line.rb:25:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:80:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:17:in `block in autorun'

  2) registrations/new stubbing current_user (uncomment the spec/support/sign_in.rb and this will work) 
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `view_context' for nil:NilClass
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-4.0.0/lib/action_view/test_case.rb:202:in `view'
     # ./spec/support/sign_in.rb:3:in `block (2 levels) in <top (required)>'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:237:in `instance_eval'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:237:in `instance_eval'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:21:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:85:in `block in run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:85:in `each'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:85:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:446:in `run_hook'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:345:in `run_before_each_hooks'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:300:in `run_before_each'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:113:in `block in run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:179:in `call'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:179:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:106:in `block (2 levels) in run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:108:in `call'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:108:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/hooks.rb:446:in `run_hook'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:340:in `run_around_each_hooks'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example.rb:111:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:390:in `block in run_examples'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:386:in `map'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:386:in `run_examples'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:371:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:372:in `block in run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:372:in `map'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/example_group.rb:372:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/command_line.rb:28:in `map'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/command_line.rb:28:in `block in run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/reporter.rb:58:in `report'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/command_line.rb:25:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:80:in `run'
     # /Users/dnagir/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:17:in `block in autorun'

Finished in 0.00156 seconds
2 examples, 2 failures

Failed examples:

rspec ./spec/views/registrations.html.erb_spec.rb:7 # registrations/new rspec doesn't provide helper methods from ApplicationController so we stub while configuring 
rspec ./spec/views/registrations.html.erb_spec.rb:12 # registrations/new stubbing current_user (uncomment the spec/support/sign_in.rb and this will work) 

Randomized with seed 33032

@dnagir
Copy link
Author

dnagir commented Oct 9, 2013

Looks like this action_view commit could be a problem rails/rails@daada51

But I'm not sure how the before hooks should be used from the configure blocks now in rspec-rails??

dnagir referenced this pull request in rails/rails Oct 9, 2013
…nment more similar to the code applications uses
@dnagir
Copy link
Author

dnagir commented Oct 9, 2013

Now that I pointed out the offending commit on Rails side it is worth mentioning that it should not be rails issue because that commit is 3 years old and worked as expected in rspec-rails 2.13, but failed in 2.14.

So I'm guessing that rspec-rails 2.14 sets up the @controller variable after the configuration has been done while the 2.13 does it before which makes it impossible to access the view from the before hooks while configuring.

Phew. Hope you can follow me. That is as much info as I can provide at this stage.

@JonRowe
Copy link
Member

JonRowe commented Oct 9, 2013

Thanks, I'll take a look later today.

@JonRowe
Copy link
Member

JonRowe commented Oct 9, 2013

Hi, ok, this is appears to have been anecdotally fixed in the latest version. Can you try using gem 'rspec-rails', github: 'rspec/rspec-rails', branch: '2-14-maintenance' in your Gemfile for me?

@JonRowe
Copy link
Member

JonRowe commented Oct 9, 2013

Note I've added a spec here which we can use to prevent a future regression.

@dnagir
Copy link
Author

dnagir commented Oct 10, 2013

@JonRowe still getting the error. The Gemfile diff is:

> git diff
diff --git a/Gemfile b/Gemfile
index 67a6816..731abf4 100644
--- a/Gemfile
+++ b/Gemfile
@@ -93,7 +93,7 @@ end


 group :development, :test do
-  gem 'rspec-rails', "2.13.2",    :require => false # https://github.com/rspec/rspec-rails/issues/833#issuecomment-25940784
+  gem 'rspec-rails', github: 'rspec/rspec-rails', branch: '2-14-maintenance', :require => false # https://github.com/rspec/rspec-rails/issues/833#issuecomment-25940784
   gem 'pry',            :require => false
   gem 'pry-remote',     :require => false
   gem 'pry-nav',        :require => false
diff --git a/Gemfile.lock b/Gemfile.lock
index 403912c..971ac74 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -33,6 +33,20 @@ GIT
       sprockets (~> 2.8)

 GIT
+  remote: git://github.com/rspec/rspec-rails.git
+  revision: 43bced5ab611cc3241721011971c7c193049aad7
+  branch: 2-14-maintenance
+  specs:
+    rspec-rails (2.14.0)
+      actionpack (>= 3.0)
+      activemodel (>= 3.0)
+      activesupport (>= 3.0)
+      railties (>= 3.0)
+      rspec-core (~> 2.14.0)
+      rspec-expectations (~> 2.14.0)
+      rspec-mocks (~> 2.14.0)
+
+GIT
   remote: git://github.com/thomas-mcdonald/bootstrap-sass.git
   revision: 8ed40df514914d643b3d76093b1c3b3ee0559935
   ref: 8ed40df514914d643b3d76093b1c3b3ee0559935
@@ -370,22 +384,15 @@ GEM
       nokogiri
       rubyzip
       spreadsheet (> 0.6.4)
-    rspec (2.13.0)
-      rspec-core (~> 2.13.0)
-      rspec-expectations (~> 2.13.0)
-      rspec-mocks (~> 2.13.0)
-    rspec-core (2.13.1)
-    rspec-expectations (2.13.0)
+    rspec (2.14.1)
+      rspec-core (~> 2.14.0)
+      rspec-expectations (~> 2.14.0)
+      rspec-mocks (~> 2.14.0)
+    rspec-core (2.14.5)
+    rspec-expectations (2.14.3)
       diff-lcs (>= 1.1.3, < 2.0)
     rspec-instafail (0.2.4)
-    rspec-mocks (2.13.1)
-    rspec-rails (2.13.2)
-      actionpack (>= 3.0)
-      activesupport (>= 3.0)
-      railties (>= 3.0)
-      rspec-core (~> 2.13.0)
-      rspec-expectations (~> 2.13.0)
-      rspec-mocks (~> 2.13.0)
+    rspec-mocks (2.14.3)
     ruby-ole (1.2.11.7)
     ruby-prof (0.13.0)
     ruby-progressbar (1.2.0)
@@ -546,7 +553,7 @@ DEPENDENCIES
   rainbows
   redcarpet
   roo
-  rspec-rails (= 2.13.2)
+  rspec-rails!
   ruby-prof
   rufus-scheduler
   safety_mailer

@JonRowe
Copy link
Member

JonRowe commented Oct 10, 2013

What version of Rails are you using, the Travis build is actually flagging up that Rails 4 is broken with this, but not 3, so I was going to investigate that later today

@dnagir
Copy link
Author

dnagir commented Oct 10, 2013

Yeah, using rails 4.0.0 here.

@JonRowe
Copy link
Member

JonRowe commented Oct 14, 2013

Hey @dnagir ok I eventually replicated your issue, this seems to have solved it, can you double check against this branch?

gem 'rspec-rails', github: 'rspec/rspec-rails', branch: 'verify_views_are_accessible_to_hook'

@dnagir
Copy link
Author

dnagir commented Oct 14, 2013

Thanks @JonRowe

Tried to bundle update rspec-rails pointed to the branch, but getting the error below. Very strange. Something isn't right about that branch.

> bundle update rspec-rails
Updating git://github.com/rspec/rspec-rails.git
fatal: ambiguous argument 'verify_views_are_accessible_to_hook': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions
Git error: command `git rev-parse verify_views_are_accessible_to_hook` in directory
/Users/dnagir/.rvm/gems/ruby-2.0.0-p195/cache/bundler/git/rspec-rails-863ffee12bd6030ece577b07c39297c14aacad85 has
failed.
If this error persists you could try removing the cache directory
'/Users/dnagir/.rvm/gems/ruby-2.0.0-p195/cache/bundler/git/rspec-rails-863ffee12bd6030ece577b07c39297c14aacad85'

(tried to remove the cache directory as suggested, but still getting the same error)

@JonRowe
Copy link
Member

JonRowe commented Oct 14, 2013

Sorry, my bad, typo.

gem 'rspec-rails', github: 'rspec/rspec-rails', branch: 'verify_views_are_accessible_to_hooks' (note the s ;) )

@dnagir
Copy link
Author

dnagir commented Oct 14, 2013

Ahh. I checked that it exists, but didn't notice the "s" it either :)

Yeah, that branch works fine with Rails 4.

Is that branch going to be long lived or I better wait for the rspec-rails release?

JonRowe added a commit that referenced this pull request Oct 14, 2013
Cannot stub view methods in before blocks (undefined method `view_context' for nil:NilClass)
@JonRowe JonRowe merged commit 4256fd4 into 2-14-maintenance Oct 14, 2013
@JonRowe JonRowe deleted the verify_views_are_accessible_to_hooks branch October 14, 2013 05:01
@JonRowe
Copy link
Member

JonRowe commented Oct 14, 2013

It's been merged into 2-14-maintenance at some point soon we'll cut a patch release (/cc @alindeman)

@dnagir
Copy link
Author

dnagir commented Oct 14, 2013

Thank @JonRowe. Well done.

@reidab
Copy link

reidab commented Dec 12, 2013

I'd also been running into this issue and was glad to see this PR. Thanks!

It seems like these changes should probably also make their way into the master branch at some point. I just ran my suite against 3.0.0.beta1 and hit the same issue.

JonRowe added a commit that referenced this pull request Dec 12, 2013
JonRowe added a commit that referenced this pull request Dec 12, 2013
Cannot stub view methods in before blocks (undefined method `view_context' for nil:NilClass)
Conflicts:
	lib/rspec/rails/adapters.rb
JonRowe added a commit that referenced this pull request Dec 12, 2013
JonRowe added a commit that referenced this pull request Dec 14, 2013
…ks_299

Merge pull request #833 from rspec/verify_views_are_accessible_to_hooks
@fotanus
Copy link

fotanus commented Feb 20, 2014

👍 for release :-)

@fotanus
Copy link

fotanus commented Feb 20, 2014

Except that don't fix my issue.

I pointed my Gemfile to 2-14-maintance branch, and still having the problem.

This is my Gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (3.2.13)
      actionpack (= 3.2.13)
      mail (~> 2.5.3)
    actionpack (3.2.13)
      activemodel (= 3.2.13)
      activesupport (= 3.2.13)
      builder (~> 3.0.0)
      erubis (~> 2.7.0)
      journey (~> 1.0.4)
      rack (~> 1.4.5)
      rack-cache (~> 1.2)
      rack-test (~> 0.6.1)
      sprockets (~> 2.2.1)
    activemodel (3.2.13)
      activesupport (= 3.2.13)
      builder (~> 3.0.0)
    activerecord (3.2.13)
      activemodel (= 3.2.13)
      activesupport (= 3.2.13)
      arel (~> 3.0.2)
      tzinfo (~> 0.3.29)
    activeresource (3.2.13)
      activemodel (= 3.2.13)
      activesupport (= 3.2.13)
    activesupport (3.2.13)
      i18n (= 0.6.1)
      multi_json (~> 1.0)
    addressable (2.3.5)
    arel (3.0.3)
    authlogic (3.3.0)
      activerecord (>= 3.2)
      activesupport (>= 3.2)
    bootstrap (0.0.1)
    bootstrap-datepicker-rails (1.3.0.1)
      railties (>= 3.0)
    bootstrap-sass (2.3.2.0)
      sass (~> 3.2)
    builder (3.0.4)
    cancan (1.6.10)
    capistrano (2.15.5)
      highline
      net-scp (>= 1.0.0)
      net-sftp (>= 2.0.0)
      net-ssh (>= 2.0.14)
      net-ssh-gateway (>= 1.1.0)
    capistrano-unicorn (0.2.0)
      capistrano (< 3.0)
    capybara (2.2.1)
      mime-types (>= 1.16)
      nokogiri (>= 1.3.3)
      rack (>= 1.0.0)
      rack-test (>= 0.5.4)
      xpath (~> 2.0)
    carrierwave (0.9.0)
      activemodel (>= 3.2.0)
      activesupport (>= 3.2.0)
      json (>= 1.7)
    choice (0.1.6)
    classy_enum (3.4.0)
      activerecord (>= 3.0)
    coffee-rails (3.2.2)
      coffee-script (>= 2.2.0)
      railties (~> 3.2.0)
    coffee-script (2.2.0)
      coffee-script-source
      execjs
    coffee-script-source (1.7.0)
    css_parser (1.3.5)
      addressable
    daemons (1.1.9)
    delayed_job (4.0.0)
      activesupport (>= 3.0, < 4.1)
    delayed_job_active_record (4.0.0)
      activerecord (>= 3.0, < 4.1)
      delayed_job (>= 3.0, < 4.1)
    delayed_job_web (1.2.5)
      activerecord (> 3.0.0)
      delayed_job (> 2.0.3)
      sinatra (>= 0.9.2)
    diff-lcs (1.2.5)
    docile (1.1.3)
    erubis (2.7.0)
    execjs (2.0.2)
    factory_girl (4.4.0)
      activesupport (>= 3.0.0)
    factory_girl_rails (4.4.0)
      factory_girl (~> 4.4.0)
      railties (>= 3.0.0)
    haml (4.0.5)
      tilt
    haml-rails (0.4)
      actionpack (>= 3.1, < 4.1)
      activesupport (>= 3.1, < 4.1)
      haml (>= 3.1, < 4.1)
      railties (>= 3.1, < 4.1)
    highline (1.6.20)
    hike (1.2.3)
    i18n (0.6.1)
    jbuilder (2.0.3)
      activesupport (>= 3.0.0)
      multi_json (>= 1.2.0)
    journey (1.0.4)
    jquery-rails (3.1.0)
      railties (>= 3.0, < 5.0)
      thor (>= 0.14, < 2.0)
    json (1.8.1)
    kgio (2.9.2)
    libv8 (3.16.14.3)
    mail (2.5.4)
      mime-types (~> 1.16)
      treetop (~> 1.4.8)
    mime-types (1.25.1)
    mini_portile (0.5.2)
    multi_json (1.8.4)
    mysql2 (0.3.15)
    net-scp (1.1.2)
      net-ssh (>= 2.6.5)
    net-sftp (2.1.2)
      net-ssh (>= 2.6.5)
    net-ssh (2.8.0)
    net-ssh-gateway (1.2.0)
      net-ssh (>= 2.6.5)
    nokogiri (1.6.1)
      mini_portile (~> 0.5.0)
    polyamorous (0.6.4)
      activerecord (>= 3.0)
    polyglot (0.3.4)
    quiet_assets (1.0.2)
      railties (>= 3.1, < 5.0)
    rack (1.4.5)
    rack-cache (1.2)
      rack (>= 0.4)
    rack-protection (1.5.2)
      rack
    rack-ssl (1.3.3)
      rack
    rack-test (0.6.2)
      rack (>= 1.0)
    rails (3.2.13)
      actionmailer (= 3.2.13)
      actionpack (= 3.2.13)
      activerecord (= 3.2.13)
      activeresource (= 3.2.13)
      activesupport (= 3.2.13)
      bundler (~> 1.0)
      railties (= 3.2.13)
    rails-erd (1.1.0)
      activerecord (>= 3.0)
      activesupport (>= 3.0)
      choice (~> 0.1.6)
      ruby-graphviz (~> 1.0.4)
    railties (3.2.13)
      actionpack (= 3.2.13)
      activesupport (= 3.2.13)
      rack-ssl (~> 1.3.2)
      rake (>= 0.8.7)
      rdoc (~> 3.4)
      thor (>= 0.14.6, < 2.0)
    raindrops (0.13.0)
    rake (10.1.1)
    ransack (1.1.0)
      actionpack (>= 3.0)
      activerecord (>= 3.0)
      polyamorous (~> 0.6.0)
    rdoc (3.12.2)
      json (~> 1.4)
    ref (1.0.5)
    rest-client (1.6.7)
      mime-types (>= 1.16)
    roadie (2.4.3)
      actionmailer (> 3.0.0, < 5.0.0)
      css_parser (~> 1.3.4)
      nokogiri (> 1.5.0)
      sprockets
    rspec-core (2.14.7)
    rspec-expectations (2.14.5)
      diff-lcs (>= 1.1.3, < 2.0)
    rspec-mocks (2.14.6)
    ruby-graphviz (1.0.9)
    rubyzip (0.9.9)
    sass (3.2.14)
    sass-rails (3.2.6)
      railties (~> 3.2.0)
      sass (>= 3.1.10)
      tilt (~> 1.3)
    shoulda-matchers (2.5.0)
      activesupport (>= 3.0.0)
    simple_form (2.0.4)
      actionpack (~> 3.0)
      activemodel (~> 3.0)
    simplecov (0.8.2)
      docile (~> 1.1.0)
      multi_json
      simplecov-html (~> 0.8.0)
    simplecov-html (0.8.0)
    sinatra (1.4.4)
      rack (~> 1.4)
      rack-protection (~> 1.4)
      tilt (~> 1.3, >= 1.3.4)
    sprockets (2.2.2)
      hike (~> 1.2)
      multi_json (~> 1.0)
      rack (~> 1.0)
      tilt (~> 1.1, != 1.3.0)
    therubyracer (0.12.1)
      libv8 (~> 3.16.14.0)
      ref
    thor (0.18.1)
    tilt (1.4.1)
    treetop (1.4.15)
      polyglot
      polyglot (>= 0.3.1)
    tzinfo (0.3.38)
    uglifier (2.4.0)
      execjs (>= 0.3.0)
      json (>= 1.8.0)
    unicorn (4.8.2)
      kgio (~> 2.6)
      rack
      raindrops (~> 0.7)
    will_paginate (3.0.5)
    xpath (2.0.0)
      nokogiri (~> 1.3)

PLATFORMS
  ruby

DEPENDENCIES
  authlogic
  bootstrap (= 0.0.1)
  bootstrap-datepicker-rails (= 1.3.0.1)
  bootstrap-sass (= 2.3.2.0)
  cancan
  capistrano
  capistrano-unicorn
  capybara
  carrierwave
  classy_enum
  coffee-rails (~> 3.2.1)
  daemons
  delayed_job_active_record
  delayed_job_web
  factory_girl
  factory_girl_rails
  haml
  haml-rails
  jbuilder
  jquery-rails
  mysql2
  quiet_assets
  rails (= 3.2.13)
  rails-erd
  ransack
  rest-client
  roadie
  rspec-rails!
  rubyzip (~> 0.9.8)
  sass
  sass-rails (~> 3.2.3)
  shoulda-matchers
  simple_form (~> 2.0.0.rc)
  simplecov
  therubyracer
  uglifier (>= 1.0.3)
  unicorn
  will_paginate

My test:

  describe '#auto_retry_stalled_objects' do
    controller do
      skip_authorization_check :stalled_error
      def stalled_error
        begin
          raise ActiveRecord::StaleObjectError.new(
            FactoryGirl.create(:job), 'to_s'
          )
        rescue ActiveRecord::StaleObjectError
          render nothing: true
        end
      end
    end

    before do
      @routes.draw do
        get 'anonymous/stalled_error'
      end
    end

    describe 'it retries' do
      before(:each) { get :stalled_error }
      it 'calls auto_retry_stalled_objects 5 times' do
        expect_any_instance_of(ApplicationController).to receive(
          :auto_retry_stalled_objects
        ).exactly(5).times

      end
    end
  end

Trying to test this method:

class ApplicationController < ActionController::Base
  around_filter :auto_retry_stalled_objects

    def auto_retry_stalled_objects
      retries = 0
      max_retries = 5
      begin
        yield
      rescue ActiveRecord::StaleObjectError => exception
        raise if retries > max_retries
        retries += 1
        log_msg = "Stalled #{exception.record}. Retry #{retries}/#{max_retries}"
        Rails.logger.warn(log_msg)
        sleep rand
        exception.record.reload
        retry
      end
    end
end

The last two, of course, are not the full files

@JonRowe
Copy link
Member

JonRowe commented Feb 21, 2014

@fotanus This has been released in 2.14.1 and in 3.0.0.beta2

@fotanus
Copy link

fotanus commented Feb 21, 2014

@JonRowe But I'm using 2.14.7 by looking at my gemspec, don't I?

Is 3.0.0 backwards compatible?

@soulcutter
Copy link
Member

@fotanus 3.0.0 is not backwards-compatible, however 2.99 should be backwards-compatible and give you deprecation warnings about things which are changing in RSpec 3.0.0 . Additionally

https://github.com/yujinakayama/transpec is a very good utility that may help you convert an existing project to 3.0.0 syntax.

@myronmarston
Copy link
Member

@JonRowe But I'm using 2.14.7 by looking at my gemspec, don't I?

There is no rspec-rails 2.14.7. That's rspec-core 2.14.7.

@soulcutter
Copy link
Member

@fotanus Also 2.14.7 is probably your version of rspec-core, there is no 2.14.7 version of rspec-rails

@JonRowe
Copy link
Member

JonRowe commented Feb 25, 2014

Sadly the .lock file doesn't specify which version of rspec-rails he's using, because git.

@fotanus
Copy link

fotanus commented Feb 25, 2014

@JonRowe @soulcutter @myronmarston Bughuting night? :-)

Sorry, you guys are right, this is the rspec-rails I'm using 2.14.1, uninstalled all other versions I had, and still the same bug. I'll try right now to set rspec 3.0.0 and see if this test, at least, runs.

@fotanus
Copy link

fotanus commented Feb 25, 2014

@JonRowe @soulcutter @myronmarston hum, no branch for rspec 3.0.0?

@myronmarston
Copy link
Member

master is the branch for RSpec 3.

Recommended upgrade process:

http://myronmars.to/n/dev-blog/2013/11/rspec-2-99-and-3-0-betas-have-been-released#the_upgrade_process

@JonRowe
Copy link
Member

JonRowe commented Feb 25, 2014

or one of the tags, or the 2.99 and 3.0 beta2 gems direct

@fotanus
Copy link

fotanus commented Feb 25, 2014

Same thing on this specific test with rspec 2.99 :(
Maybe I'm doing something wrong? Full test and method being tested above.

  Failure/Error: Unable to find matching line from backtrace
       Exactly one instance should have received the following message(s) but didn't: auto_retry_stalled_objects
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/pry-exception_explorer-0.1.9/lib/pry-exception_explorer/core_ext.rb:46:in `raise'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-mocks-2.99.0.beta2/lib/rspec/mocks/any_instance/recorder.rb:89:in `verify'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-mocks-2.99.0.beta2/lib/rspec/mocks/space.rb:20:in `block in verify_all'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-mocks-2.99.0.beta2/lib/rspec/mocks/space.rb:19:in `each_value'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-mocks-2.99.0.beta2/lib/rspec/mocks/space.rb:19:in `verify_all'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-mocks-2.99.0.beta2/lib/rspec/mocks.rb:28:in `verify'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/mocking/with_rspec.rb:18:in `verify_mocks_for_rspec'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/example.rb:304:in `verify_mocks'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/example.rb:296:in `run_after_each'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/example.rb:120:in `block in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/bundler/gems/rspec-rails-95585765359e/lib/rspec/rails/example/controller_example_group.rb:159:in `call'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/bundler/gems/rspec-rails-95585765359e/lib/rspec/rails/example/controller_example_group.rb:159:in `block (2 levels) in <module:ControllerExampleGroup>'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/example.rb:244:in `instance_eval_with_args'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/hooks.rb:106:in `block (2 levels) in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/hooks.rb:108:in `call'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/hooks.rb:108:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/hooks.rb:446:in `run_hook'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/example_group.rb:413:in `run_around_each_hooks'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/example.rb:253:in `with_around_each_hooks'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/example.rb:111:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/example_group.rb:465:in `block in run_examples'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/example_group.rb:461:in `map'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/example_group.rb:461:in `run_examples'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/example_group.rb:446:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/example_group.rb:447:in `block in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/example_group.rb:447:in `map'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/example_group.rb:447:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/example_group.rb:447:in `block in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/example_group.rb:447:in `map'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/example_group.rb:447:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/command_line.rb:28:in `map'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/command_line.rb:28:in `block in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/reporter.rb:58:in `report'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/command_line.rb:25:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/runner.rb:89:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247@translation_proxy/gems/rspec-core-2.99.0.beta2/lib/rspec/core/runner.rb:17:in `block in autorun'

Now I'm running all test suit to check for deprecations, as the procedure pointed, after all, I'll end up upgrading even if not fix my issue

@JonRowe
Copy link
Member

JonRowe commented Feb 25, 2014

That's not an error related to this issue... Exactly one instance should have received the following message(s) but didn't: auto_retry_stalled_objects

@JonRowe
Copy link
Member

JonRowe commented Feb 25, 2014

This issue is about view specs

@fotanus
Copy link

fotanus commented Feb 25, 2014

I see. My bad. Feel free to close this thing again.
I'll try to find out what is wrong on my code.

@JonRowe
Copy link
Member

JonRowe commented Feb 25, 2014

Looking at your code, your error is because you haven't actually made a request, so your expectation fails.

@JonRowe
Copy link
Member

JonRowe commented Feb 25, 2014

Also in future, please add such files into gists rather than placing the entire contents in a comment thread.

@fotanus
Copy link

fotanus commented Feb 25, 2014

@JonRowe you are correct, thanks! Shame on me.
Sorry abut the chunk of code, I'll add as a gist next time.

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

Successfully merging this pull request may close these issues.

6 participants