Permalink
Browse files

Automatically include ancestors helpers into view specs

  • Loading branch information...
1 parent 1d15db0 commit d3caa8b990d85557a7fbd2a4f801bed8ed2f8ce1 @atambo committed Dec 14, 2012
Showing with 38 additions and 53 deletions.
  1. +19 −6 lib/rspec/rails/example/view_example_group.rb
  2. +19 −47 spec/rspec/rails/example/view_example_group_spec.rb
@@ -9,17 +9,30 @@ module ViewExampleGroup
include RSpec::Rails::Matchers::RenderTemplate
module ClassMethods
- def _default_helper
- base = metadata[:example_group][:description].split('/')[0..-2].join('/')
- (base.camelize + 'Helper').constantize if base
+ def _base
+ metadata[:example_group][:description].split('/')[0..-2].join('/').camelize
+ end
+
+ def _controller
+ (_base + 'Controller').constantize
+ rescue NameError
+ nil
+ end
+
+ def _helper(name)
+ base_name = name.sub(/Controller$/,'')
+ (base_name + 'Helper').constantize
rescue NameError
nil
end
def _default_helpers
- helpers = [_default_helper].compact
- helpers << ApplicationHelper if Object.const_defined?('ApplicationHelper')
- helpers
+ helpers = []
+ _controller.ancestors.each do |ancestor|
+ break if ancestor == ActionController::Base
+ helpers << _helper(ancestor.name)
+ end if _controller
+ helpers.compact
end
end
@@ -13,8 +13,8 @@ module RSpec::Rails
end
describe 'automatic inclusion of helpers' do
+ class ::ThingsController < ActionController::Base; end
module ::ThingsHelper; end
- module ::Namespaced; module ThingsHelper; end; end
it 'includes the helper with the same name' do
group = RSpec::Core::ExampleGroup.describe 'things/show.html.erb'
@@ -24,6 +24,11 @@ module ::Namespaced; module ThingsHelper; end; end
end
end
+ module ::Namespaced
+ class ThingsController < ActionController::Base; end
+ module ThingsHelper; end
+ end
+
it 'includes the namespaced helper with the same name' do
group = RSpec::Core::ExampleGroup.describe 'namespaced/things/show.html.erb'
group.should_receive(:helper).with(Namespaced::ThingsHelper)
@@ -32,6 +37,19 @@ module ::Namespaced; module ThingsHelper; end; end
end
end
+ class ::ParentController < ActionController::Base; end
+ class ::ChildController < ::ParentController; end
+ module ::ParentHelper; end
+ module ::ChildHelper; end
+
+ it 'includes the ancestor helper with the same name' do
+ group = RSpec::Core::ExampleGroup.describe 'child/show.html.erb'
+ group.should_receive(:helper).with(ChildHelper, ParentHelper)
+ group.class_eval do
+ include ViewExampleGroup
+ end
+ end
+
it 'operates normally when no helper with the same name exists' do
raise 'unexpected constant found' if Object.const_defined?('ClocksHelper')
lambda {
@@ -40,52 +58,6 @@ module ::Namespaced; module ThingsHelper; end; end
end
}.should_not raise_error
end
-
- context 'application helper exists' do
- before do
- if !Object.const_defined? 'ApplicationHelper'
- module ::ApplicationHelper; end
- @application_helper_defined = true
- end
- end
-
- after do
- if @application_helper_defined
- Object.__send__ :remove_const, 'ApplicationHelper'
- end
- end
-
- it 'includes the application helper' do
- group = RSpec::Core::Example.describe 'bars/new.html.erb'
- group.should_receive(:helper).with(ApplicationHelper)
- group.class_eval do
- include ViewExampleGroup
- end
- end
- end
-
- context 'no application helper exists' do
- before do
- if Object.const_defined? 'ApplicationHelper'
- @application_helper = ApplicationHelper
- Object.__send__ :remove_const, 'ApplicationHelper'
- end
- end
-
- after do
- if @application_helper
- ApplicationHelper = @application_helper
- end
- end
-
- it 'operates normally' do
- lambda {
- RSpec::Core::ExampleGroup.describe 'foos/edit.html.erb' do
- include ViewExampleGroup
- end
- }.should_not raise_error
- end
- end
end
describe "#render" do

0 comments on commit d3caa8b

Please sign in to comment.