Skip to content

Instantly share code, notes, and snippets.

@dchelimsky
Forked from wincent/gist:476715
Created July 15, 2010 12:48
Show Gist options
  • Save dchelimsky/476885 to your computer and use it in GitHub Desktop.
Save dchelimsky/476885 to your computer and use it in GitHub Desktop.
diff --git a/features/view_specs/view_spec.feature b/features/view_specs/view_spec.feature
index e85c306..6534c30 100644
--- a/features/view_specs/view_spec.feature
+++ b/features/view_specs/view_spec.feature
@@ -152,3 +152,34 @@ Feature: view spec
"""
When I run "rspec spec/views"
Then the output should contain "1 example, 0 failures"
+
+ Scenario: spec with view that accesses helper_method helpers
+ Given a file named "app/views/secrets/index.html.erb" with:
+ """
+ <%- if admin? %>
+ <h1>Secret admin area</h1>
+ <%- end %>
+ """
+ And a file named "spec/views/secrets/index.html.erb_spec.rb" with:
+ """
+ require 'spec_helper'
+
+ describe 'secrets/index.html.erb' do
+ before do
+ controller.singleton_class.class_eval do
+ protected
+ def admin?
+ true
+ end
+ helper_method :admin?
+ end
+ end
+
+ it 'checks for admin access' do
+ render
+ rendered.should contain('Secret admin area')
+ end
+ end
+ """
+ When I run "rspec spec/views/secrets"
+ Then the output should contain "1 example, 0 failures"
diff --git a/lib/rspec/rails/example/view_example_group.rb b/lib/rspec/rails/example/view_example_group.rb
index 40e371a..efabcc7 100644
--- a/lib/rspec/rails/example/view_example_group.rb
+++ b/lib/rspec/rails/example/view_example_group.rb
@@ -102,7 +102,6 @@ module RSpec::Rails
view
end
-
# Deprecated. Use +rendered+ instead.
def response
RSpec.deprecate("response", "rendered")
@@ -118,6 +117,13 @@ module RSpec::Rails
def _controller_path
_default_file_to_render.split("/")[0..-2].join("/")
end
+
+ def _include_controller_helpers
+ helpers = controller._helpers
+ view.singleton_class.class_eval do
+ include helpers unless included_modules.include?(helpers)
+ end
+ end
end
included do
@@ -125,6 +131,7 @@ module RSpec::Rails
helper *_default_helpers
before do
+ _include_controller_helpers
controller.controller_path = _controller_path
# this won't be necessary if/when
# https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4903
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment