This repository is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are added as a member.
Every repository with this icon (
) is private.
Every repository with this icon (
This repository is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
| 6d673c20 » | btakita | 2007-11-25 | 1 | module Spec | |
| 2 | module Rails | ||||
| 3 | module Example | ||||
| 4 | # View Examples live in $RAILS_ROOT/spec/views/. | ||||
| 5 | # | ||||
| 6 | # View Specs use Spec::Rails::Example::ViewExampleGroup, | ||||
| 7 | # which provides access to views without invoking any of your controllers. | ||||
| 8 | # See Spec::Rails::Expectations::Matchers for information about specific | ||||
| 9 | # expectations that you can set on views. | ||||
| 10 | # | ||||
| 11 | # == Example | ||||
| 12 | # | ||||
| 13 | # describe "login/login" do | ||||
| 14 | # before do | ||||
| 15 | # render 'login/login' | ||||
| 16 | # end | ||||
| 17 | # | ||||
| 18 | # it "should display login form" do | ||||
| 19 | # response.should have_tag("form[action=/login]") do | ||||
| 20 | # with_tag("input[type=text][name=email]") | ||||
| 21 | # with_tag("input[type=password][name=password]") | ||||
| 22 | # with_tag("input[type=submit][value=Login]") | ||||
| 23 | # end | ||||
| 24 | # end | ||||
| 25 | # end | ||||
| 26 | class ViewExampleGroup < FunctionalExampleGroup | ||||
| 27 | before(:each) do | ||||
| 28 | ensure_that_flash_and_session_work_properly | ||||
| 29 | end | ||||
| 30 | |||||
| 05f68694 » | dchelimsky | 2008-01-18 | 31 | after(:each) do | |
| 32 | ensure_that_base_view_path_is_not_set_across_example_groups | ||||
| 33 | end | ||||
| 34 | |||||
| d7c0773a » | dchelimsky | 2008-10-06 | 35 | def initialize(defined_description, options={}, &implementation) #:nodoc: | |
| 6d673c20 » | btakita | 2007-11-25 | 36 | super | |
| 37 | @controller_class_name = "Spec::Rails::Example::ViewExampleGroupController" | ||||
| 38 | end | ||||
| 39 | |||||
| 40 | def ensure_that_flash_and_session_work_properly #:nodoc: | ||||
| 41 | @controller.send :initialize_template_class, @response | ||||
| 42 | @controller.send :assign_shortcuts, @request, @response | ||||
| 43 | @session = @controller.session | ||||
| 44 | @controller.class.send :public, :flash | ||||
| 45 | end | ||||
| 46 | |||||
| 55c7bfad » | btakita | 2007-11-25 | 47 | def ensure_that_base_view_path_is_not_set_across_example_groups #:nodoc: | |
| 6d673c20 » | btakita | 2007-11-25 | 48 | ActionView::Base.base_view_path = nil | |
| 49 | end | ||||
| 50 | |||||
| 51 | def set_base_view_path(options) #:nodoc: | ||||
| 52 | ActionView::Base.base_view_path = base_view_path(options) | ||||
| 53 | end | ||||
| 54 | |||||
| 55 | def base_view_path(options) #:nodoc: | ||||
| 56 | "/#{derived_controller_name(options)}/" | ||||
| 57 | end | ||||
| 58 | |||||
| 59 | def derived_controller_name(options) #:nodoc: | ||||
| 60 | parts = subject_of_render(options).split('/').reject { |part| part.empty? } | ||||
| 61 | "#{parts[0..-2].join('/')}" | ||||
| 62 | end | ||||
| 63 | |||||
| 64 | def derived_action_name(options) #:nodoc: | ||||
| 65 | parts = subject_of_render(options).split('/').reject { |part| part.empty? } | ||||
| ff345085 » | shame | 2008-10-02 | 66 | "#{parts.last}".split('.').first | |
| 6d673c20 » | btakita | 2007-11-25 | 67 | end | |
| 68 | |||||
| 69 | def subject_of_render(options) #:nodoc: | ||||
| 70 | [:template, :partial, :file].each do |render_type| | ||||
| 71 | if options.has_key?(render_type) | ||||
| 72 | return options[render_type] | ||||
| 73 | end | ||||
| 74 | end | ||||
| 8d46eff4 » | dchelimsky | 2008-02-10 | 75 | return "" | |
| 6d673c20 » | btakita | 2007-11-25 | 76 | end | |
| 77 | |||||
| 78 | def add_helpers(options) #:nodoc: | ||||
| 79 | @controller.add_helper("application") | ||||
| 80 | @controller.add_helper(derived_controller_name(options)) | ||||
| 81 | @controller.add_helper(options[:helper]) if options[:helper] | ||||
| 82 | options[:helpers].each { |helper| @controller.add_helper(helper) } if options[:helpers] | ||||
| 83 | end | ||||
| 84 | |||||
| 85 | # Renders a template for a View Spec, which then provides access to the result | ||||
| 8d46eff4 » | dchelimsky | 2008-02-10 | 86 | # through the +response+. Also supports render with :inline, which you can | |
| 87 | # use to spec custom form builders, helpers, etc, in the context of a view. | ||||
| 6d673c20 » | btakita | 2007-11-25 | 88 | # | |
| 89 | # == Examples | ||||
| 90 | # | ||||
| 91 | # render('/people/list') | ||||
| 92 | # render('/people/list', :helper => MyHelper) | ||||
| 93 | # render('/people/list', :helpers => [MyHelper, MyOtherHelper]) | ||||
| 94 | # render(:partial => '/people/_address') | ||||
| 8d46eff4 » | dchelimsky | 2008-02-10 | 95 | # render(:inline => "<% custom_helper 'argument', 'another argument' %>") | |
| 6d673c20 » | btakita | 2007-11-25 | 96 | # | |
| 97 | # See Spec::Rails::Example::ViewExampleGroup for more information. | ||||
| 98 | def render(*args) | ||||
| 99 | options = Hash === args.last ? args.pop : {} | ||||
| 100 | options[:template] = args.first.to_s unless args.empty? | ||||
| 101 | |||||
| 102 | set_base_view_path(options) | ||||
| 103 | add_helpers(options) | ||||
| 104 | |||||
| 105 | assigns[:action_name] = @action_name | ||||
| 106 | |||||
| fb8468b2 » | shame | 2008-08-07 | 107 | @request.path_parameters = @request.path_parameters.update( | |
| 0461ecfe » | dchelimsky | 2008-11-21 | 108 | :controller => options[:controller] || derived_controller_name(options), | |
| 109 | :action => options[:action] || derived_action_name(options) | ||||
| fb8468b2 » | shame | 2008-08-07 | 110 | ) | |
| 6d673c20 » | btakita | 2007-11-25 | 111 | ||
| 112 | defaults = { :layout => false } | ||||
| 113 | options = defaults.merge options | ||||
| 114 | |||||
| 236a48b8 » | patmaddox | 2008-04-04 | 115 | @controller.send(:params).reverse_merge! @request.parameters | |
| 6d673c20 » | btakita | 2007-11-25 | 116 | ||
| 117 | @controller.send :initialize_current_url | ||||
| 118 | |||||
| 119 | @controller.class.instance_eval %{ | ||||
| 120 | def controller_path | ||||
| 121 | "#{derived_controller_name(options)}" | ||||
| 122 | end | ||||
| 123 | |||||
| 124 | def controller_name | ||||
| 125 | "#{derived_controller_name(options).split('/').last}" | ||||
| 126 | end | ||||
| 127 | } | ||||
| 128 | |||||
| 129 | @controller.send :forget_variables_added_to_assigns | ||||
| 130 | @controller.send :render, options | ||||
| 131 | @controller.send :process_cleanup | ||||
| 132 | end | ||||
| 133 | |||||
| 134 | # This provides the template. Use this to set mock | ||||
| 135 | # expectations for dealing with partials | ||||
| 136 | # | ||||
| 137 | # == Example | ||||
| 138 | # | ||||
| 139 | # describe "/person/new" do | ||||
| 140 | # it "should use the form partial" do | ||||
| 141 | # template.should_receive(:render).with(:partial => 'form') | ||||
| 142 | # render "/person/new" | ||||
| 143 | # end | ||||
| 144 | # end | ||||
| 145 | def template | ||||
| 146 | @controller.template | ||||
| 147 | end | ||||
| 148 | |||||
| 149 | Spec::Example::ExampleGroupFactory.register(:view, self) | ||||
| 8bffabeb » | dchelimsky | 2008-02-17 | 150 | ||
| 151 | protected | ||||
| 152 | def _assigns_hash_proxy | ||||
| 5bb22b4d » | dchelimsky | 2008-09-09 | 153 | @_assigns_hash_proxy ||= AssignsHashProxy.new self do | |
| 154 | @response.template | ||||
| 155 | end | ||||
| 8bffabeb » | dchelimsky | 2008-02-17 | 156 | end | |
| 6d673c20 » | btakita | 2007-11-25 | 157 | end | |
| 158 | |||||
| 159 | class ViewExampleGroupController < ApplicationController #:nodoc: | ||||
| 160 | include Spec::Rails::Example::RenderObserver | ||||
| 161 | attr_reader :template | ||||
| 162 | |||||
| 163 | def add_helper_for(template_path) | ||||
| 164 | add_helper(template_path.split('/')[0]) | ||||
| 165 | end | ||||
| 166 | |||||
| 167 | def add_helper(name) | ||||
| 168 | begin | ||||
| 169 | helper_module = "#{name}_helper".camelize.constantize | ||||
| 170 | rescue | ||||
| 171 | return | ||||
| 172 | end | ||||
| d89f9ec9 » | dchelimsky | 2008-02-12 | 173 | (class << template; self; end).class_eval do | |
| 6d673c20 » | btakita | 2007-11-25 | 174 | include helper_module | |
| 175 | end | ||||
| 176 | end | ||||
| 5bb22b4d » | dchelimsky | 2008-09-09 | 177 | ||
| 178 | def forget_variables_added_to_assigns | ||||
| 179 | end | ||||
| 6d673c20 » | btakita | 2007-11-25 | 180 | end | |
| 181 | end | ||||
| 182 | end | ||||
| 183 | end | ||||







