Class: Spec::Rails::Example::ViewExampleGroup
- Inherits:
-
FunctionalExampleGroup
- Object
- ActionController::TestCase
- FunctionalExampleGroup
- Spec::Rails::Example::ViewExampleGroup
- Includes:
- ActionView::Helpers
- Defined in:
- lib/vendor/plugins/rspec-rails/lib/spec/rails/example/view_example_group.rb
Overview
View Examples live in $RAILS_ROOT/spec/views/.
View Specs use Spec::Rails::Example::ViewExampleGroup, which provides access to views without invoking any of your controllers. See Spec::Rails::Expectations::Matchers for information about specific expectations that you can set on views.
Example
describe "login/login" do
before do
render 'login/login'
end
it "should display login form" do
response.should have_tag("form[action=/login]") do
with_tag("input[type=text][name=email]")
with_tag("input[type=password][name=password]")
with_tag("input[type=submit][value=Login]")
end
end
end
Instance Attribute Summary
Attributes inherited from FunctionalExampleGroup
Class Method Summary collapse
-
.inherited(klass) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#add_helpers(options) ⇒ Object
:nodoc:.
-
#base_view_path(options) ⇒ Object
:nodoc:.
-
#derived_action_name(options) ⇒ Object
:nodoc:.
-
#derived_controller_name(options) ⇒ Object
:nodoc:.
-
#ensure_that_base_view_path_is_not_set_across_example_groups ⇒ Object
:nodoc:.
-
#ensure_that_flash_and_session_work_properly ⇒ Object
:nodoc:.
-
#render(*args) ⇒ Object
Renders a template for a View Spec, which then provides access to the result through the
response
. -
#set_base_view_path(options) ⇒ Object
:nodoc:.
-
#subject_of_render(options) ⇒ Object
:nodoc:.
-
#template ⇒ Object
This provides the template.
Methods inherited from FunctionalExampleGroup
#assigns, #cookies, #flash, #orig_assigns, #params, #session, #setup
Methods inherited from ActionController::TestCase
Methods included from RoutingHelpers
Class Method Details
.inherited(klass) ⇒ Object
:nodoc:
65 66 67 68 |
# File 'lib/vendor/plugins/rspec-rails/lib/spec/rails/example/view_example_group.rb', line 65 def inherited(klass) # :nodoc: klass.subject { template } super end |
Instance Method Details
#add_helpers(options) ⇒ Object
:nodoc:
113 114 115 116 117 118 |
# File 'lib/vendor/plugins/rspec-rails/lib/spec/rails/example/view_example_group.rb', line 113 def add_helpers() #:nodoc: @controller.add_helper("application") @controller.add_helper(derived_controller_name()) @controller.add_helper([:helper]) if [:helper] [:helpers].each { |helper| @controller.add_helper(helper) } if [:helpers] end |
#base_view_path(options) ⇒ Object
:nodoc:
90 91 92 |
# File 'lib/vendor/plugins/rspec-rails/lib/spec/rails/example/view_example_group.rb', line 90 def base_view_path() #:nodoc: "/#{derived_controller_name()}/" end |
#derived_action_name(options) ⇒ Object
:nodoc:
99 100 101 102 |
# File 'lib/vendor/plugins/rspec-rails/lib/spec/rails/example/view_example_group.rb', line 99 def derived_action_name() #:nodoc: parts = subject_of_render().split('/').reject { |part| part.empty? } "#{parts.last}".split('.').first end |
#derived_controller_name(options) ⇒ Object
:nodoc:
94 95 96 97 |
# File 'lib/vendor/plugins/rspec-rails/lib/spec/rails/example/view_example_group.rb', line 94 def derived_controller_name() #:nodoc: parts = subject_of_render().split('/').reject { |part| part.empty? } "#{parts[0..-2].join('/')}" end |
#ensure_that_base_view_path_is_not_set_across_example_groups ⇒ Object
:nodoc:
82 83 84 |
# File 'lib/vendor/plugins/rspec-rails/lib/spec/rails/example/view_example_group.rb', line 82 def ensure_that_base_view_path_is_not_set_across_example_groups #:nodoc: ActionView::Base.base_view_path = nil end |
#ensure_that_flash_and_session_work_properly ⇒ Object
:nodoc:
74 75 76 77 78 79 80 |
# File 'lib/vendor/plugins/rspec-rails/lib/spec/rails/example/view_example_group.rb', line 74 def ensure_that_flash_and_session_work_properly #:nodoc: @controller.class.__send__ :public, :flash @controller.__send__ :initialize_template_class, @response @controller.__send__ :assign_shortcuts, @request, @response @controller.__send__ :initialize_current_url @session = @controller.session end |
#render(*args) ⇒ Object
Renders a template for a View Spec, which then provides access to the result through the response
. Also supports render with :inline, which you can use to spec custom form builders, helpers, etc, in the context of a view.
Examples
render('/people/list')
render('/people/list', :helper => MyHelper)
render('/people/list', :helpers => [MyHelper, MyOtherHelper])
render(:partial => '/people/_address')
render(:inline => "<% custom_helper 'argument', 'another argument' %>")
See Spec::Rails::Example::ViewExampleGroup for more information.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/vendor/plugins/rspec-rails/lib/spec/rails/example/view_example_group.rb', line 133 def render(*args) = Hash === args.last ? args.pop : {} if args.empty? unless [:partial, :inline, :file, :template, :xml, :json, :update].any? {|k| .has_key? k} args << self.class.description_parts.first end end [:template] = args.first.to_s.sub(/^\//,'') unless args.empty? set_base_view_path() add_helpers() assigns[:action_name] = @action_name @request.path_parameters = @request.path_parameters.merge( :controller => derived_controller_name(), :action => derived_action_name() ).merge([:path_parameters] || {}) defaults = { :layout => false } = defaults.merge @controller.__send__(:params).reverse_merge! @request.parameters @controller.class.instance_eval %{ def controller_path "#{derived_controller_name()}" end def controller_name "#{derived_controller_name().split('/').last}" end } @controller.__send__ :forget_variables_added_to_assigns @controller.__send__ :render, @controller.__send__ :process_cleanup end |
#set_base_view_path(options) ⇒ Object
:nodoc:
86 87 88 |
# File 'lib/vendor/plugins/rspec-rails/lib/spec/rails/example/view_example_group.rb', line 86 def set_base_view_path() #:nodoc: ActionView::Base.base_view_path = base_view_path() end |
#subject_of_render(options) ⇒ Object
:nodoc:
104 105 106 107 108 109 110 111 |
# File 'lib/vendor/plugins/rspec-rails/lib/spec/rails/example/view_example_group.rb', line 104 def subject_of_render() #:nodoc: [:template, :partial, :file].each do |render_type| if .has_key?(render_type) return [render_type] end end return "" end |
#template ⇒ Object
This provides the template. Use this to set mock expectations for dealing with partials
Example
describe "/person/new" do
it "should use the form partial" do
template.should_receive(:render).with(:partial => 'form')
render "/person/new"
end
end
185 186 187 |
# File 'lib/vendor/plugins/rspec-rails/lib/spec/rails/example/view_example_group.rb', line 185 def template @controller.template end |