-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Cannot stub view methods in before blocks (undefined method `view_context' for nil:NilClass) #833
Conversation
Can you do me a favour and verify that the function your looking for exists in vanilla TestUnit/MiniTest? /cc @alindeman |
I'm not using minitest.
|
|
Okay. I'll try tomorrow... But the ONLY difference between working app and not is the version of
|
It's a series of interconnected parts, it helps us understand why something might not work anymore |
Ok. No worries. On 4 October 2013 14:36, Jon Rowe notifications@github.com wrote:
|
A bit more info on this issue: The minimal rails example was created with The relevant commit that reproduces is dnagir/issue833@b408cd8 For full repro just clone the repro, The problem is that the The full backtrace is:
|
Looks like this But I'm not sure how the |
…nment more similar to the code applications uses
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 So I'm guessing that Phew. Hope you can follow me. That is as much info as I can provide at this stage. |
Thanks, I'll take a look later today. |
Hi, ok, this is appears to have been anecdotally fixed in the latest version. Can you try using |
Note I've added a spec here which we can use to prevent a future regression. |
@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 |
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 |
Yeah, using rails |
Hey @dnagir ok I eventually replicated your issue, this seems to have solved it, can you double check against this branch?
|
Thanks @JonRowe Tried to > 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) |
Sorry, my bad, typo.
|
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 |
Cannot stub view methods in before blocks (undefined method `view_context' for nil:NilClass)
It's been merged into |
Thank @JonRowe. Well done. |
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 |
Cannot stub view methods in before blocks (undefined method `view_context' for nil:NilClass) Conflicts: lib/rspec/rails/adapters.rb
…ks_299 Merge pull request #833 from rspec/verify_views_are_accessible_to_hooks
👍 for release :-) |
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
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 |
@fotanus This has been released in |
@JonRowe But I'm using Is 3.0.0 backwards compatible? |
@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. |
There is no rspec-rails 2.14.7. That's rspec-core 2.14.7. |
@fotanus Also |
Sadly the |
@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. |
@JonRowe @soulcutter @myronmarston hum, no branch for rspec 3.0.0? |
Recommended upgrade process: |
or one of the tags, or the 2.99 and 3.0 beta2 gems direct |
Same thing on this specific test with rspec 2.99 :(
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 |
That's not an error related to this issue... |
This issue is about view specs |
I see. My bad. Feel free to close this thing again. |
Looking at your code, your error is because you haven't actually made a request, so your expectation fails. |
Also in future, please add such files into gists rather than placing the entire contents in a comment thread. |
@JonRowe you are correct, thanks! Shame on me. |
After upgrading rspec to 2.14 we started to get the errors:
The reason seem to be that it is not possible to stub view methods from
before
blocks.This is complete support file: