Skip to content

Commit 13b8013

Browse files
committed
Include helper methods defined in controllers in the view object in
ViewExampleGroup - based on gist from Wincent Colaiuta <win@wincent.com> - Closes #119.
1 parent 96a29c4 commit 13b8013

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

features/view_specs/view_spec.feature

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,34 @@ Feature: view spec
152152
"""
153153
When I run "rspec spec/views"
154154
Then the output should contain "1 example, 0 failures"
155+
156+
Scenario: spec with view that accesses helper_method helpers
157+
Given a file named "app/views/secrets/index.html.erb" with:
158+
"""
159+
<%- if admin? %>
160+
<h1>Secret admin area</h1>
161+
<%- end %>
162+
"""
163+
And a file named "spec/views/secrets/index.html.erb_spec.rb" with:
164+
"""
165+
require 'spec_helper'
166+
167+
describe 'secrets/index.html.erb' do
168+
before do
169+
controller.singleton_class.class_eval do
170+
protected
171+
def admin?
172+
true
173+
end
174+
helper_method :admin?
175+
end
176+
end
177+
178+
it 'checks for admin access' do
179+
render
180+
rendered.should contain('Secret admin area')
181+
end
182+
end
183+
"""
184+
When I run "rspec spec/views/secrets"
185+
Then the output should contain "1 example, 0 failures"

lib/rspec/rails/example/view_example_group.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ def template
102102
view
103103
end
104104

105-
106105
# Deprecated. Use +rendered+ instead.
107106
def response
108107
RSpec.deprecate("response", "rendered")
@@ -118,13 +117,21 @@ def _default_file_to_render
118117
def _controller_path
119118
_default_file_to_render.split("/")[0..-2].join("/")
120119
end
120+
121+
def _include_controller_helpers
122+
helpers = controller._helpers
123+
view.singleton_class.class_eval do
124+
include helpers unless included_modules.include?(helpers)
125+
end
126+
end
121127
end
122128

123129
included do
124130
metadata[:type] = :view
125131
helper *_default_helpers
126132

127133
before do
134+
_include_controller_helpers
128135
controller.controller_path = _controller_path
129136
# this won't be necessary if/when
130137
# https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4903

spec/rspec/rails/example/view_example_group_spec.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,18 @@ def _assigns
122122
end
123123

124124
describe '#params' do
125-
include ViewExampleGroup
125+
let(:view_spec) do
126+
Class.new do
127+
include ViewExampleGroup::InstanceMethods
128+
def controller
129+
@controller ||= Object.new
130+
end
131+
end.new
132+
end
133+
126134
it 'delegates to the controller' do
127-
controller.should_receive(:params).and_return({})
128-
params[:foo] = 1
135+
view_spec.controller.should_receive(:params).and_return({})
136+
view_spec.params[:foo] = 1
129137
end
130138
end
131139

0 commit comments

Comments
 (0)