Created
July 6, 2010 16:26
-
-
Save wincent/465592 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From ac79dd7c726dde04be6a1a1dfdd6c781276b31b4 Mon Sep 17 00:00:00 2001 | |
From: Wincent Colaiuta <[email protected]> | |
Date: Tue, 6 Jul 2010 17:48:20 +0200 | |
Subject: [PATCH] Make view helpers available in view specs | |
This maintains the behavior established in RSpec 1. | |
See: | |
http://github.com/rspec/rspec-rails/issues/issue/119 | |
--- | |
lib/rspec/rails/example/view_example_group.rb | 16 +++++ | |
.../rspec/rails/example/view_example_group_spec.rb | 67 ++++++++++++++++++++ | |
2 files changed, 83 insertions(+), 0 deletions(-) | |
diff --git a/lib/rspec/rails/example/view_example_group.rb b/lib/rspec/rails/example/view_example_group.rb | |
index 5874724..526ccba 100644 | |
--- a/lib/rspec/rails/example/view_example_group.rb | |
+++ b/lib/rspec/rails/example/view_example_group.rb | |
@@ -28,6 +28,20 @@ module RSpec::Rails | |
include Webrat::Matchers | |
include RSpec::Rails::Matchers::RenderTemplate | |
+ module ClassMethods | |
+ def _default_helper | |
+ (example.example_group.top_level_description.split('/').first.camelize + 'Helper').constantize | |
+ rescue NameError => e | |
+ nil | |
+ end | |
+ | |
+ def _default_helpers | |
+ helpers = [_default_helper].compact | |
+ helpers << ApplicationHelper if Object.const_defined?('ApplicationHelper') | |
+ helpers | |
+ end | |
+ end | |
+ | |
module InstanceMethods | |
# :call-seq: | |
# render | |
@@ -98,6 +112,8 @@ module RSpec::Rails | |
end | |
included do | |
+ helper *_default_helpers | |
+ | |
before do | |
controller.controller_path = _controller_path | |
# this won't be necessary if/when | |
diff --git a/spec/rspec/rails/example/view_example_group_spec.rb b/spec/rspec/rails/example/view_example_group_spec.rb | |
index d3e4376..203e7d1 100644 | |
--- a/spec/rspec/rails/example/view_example_group_spec.rb | |
+++ b/spec/rspec/rails/example/view_example_group_spec.rb | |
@@ -5,6 +5,73 @@ module RSpec::Rails | |
it { should be_included_in_files_in('./spec/views/') } | |
it { should be_included_in_files_in('.\\spec\\views\\') } | |
+ describe 'automatic inclusion of helpers' do | |
+ module ::ThingsHelper; end | |
+ | |
+ it 'includes the helper with the same name when it exists' do | |
+ group = RSpec::Core::ExampleGroup.describe 'things/show.html.erb' | |
+ group.should_receive(:helper).with(ThingsHelper) | |
+ 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 { | |
+ RSpec::Core::ExampleGroup.describe 'clocks/show.html.erb' do | |
+ include ViewExampleGroup | |
+ 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 'foos/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 | |
let(:view_spec) do | |
Class.new do | |
-- | |
1.7.1.1 |
SunDi3yansyah
commented
Dec 5, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment