changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec_on_rails/lib/spec/rails/example/view_example_group.rb

changeset 16: 01fd3f10ae84
author: moriq@moriq.com
date: Mon Mar 10 10:13:18 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec_on_rails
1module 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
31 after(:each) do
32 ensure_that_base_view_path_is_not_set_across_example_groups
33 end
34
35 def initialize(defined_description, &implementation) #:nodoc:
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
47 def ensure_that_base_view_path_is_not_set_across_example_groups #:nodoc:
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? }
66 "#{parts.last}"
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
75 raise Exception.new("Unhandled render type in view spec.")
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
86 # through the +response+.
87 #
88 # == Examples
89 #
90 # render('/people/list')
91 # render('/people/list', :helper => MyHelper)
92 # render('/people/list', :helpers => [MyHelper, MyOtherHelper])
93 # render(:partial => '/people/_address')
94 #
95 # See Spec::Rails::Example::ViewExampleGroup for more information.
96 def render(*args)
97 options = Hash === args.last ? args.pop : {}
98 options[:template] = args.first.to_s unless args.empty?
99
100 set_base_view_path(options)
101 add_helpers(options)
102
103 assigns[:action_name] = @action_name
104
105 @request.path_parameters = {
106 :controller => derived_controller_name(options),
107 :action => derived_action_name(options)
108 }
109
110 defaults = { :layout => false }
111 options = defaults.merge options
112
113 @controller.instance_variable_set :@params, @request.parameters
114
115 @controller.send :initialize_current_url
116
117 @controller.class.instance_eval %{
118 def controller_path
119 "#{derived_controller_name(options)}"
120 end
121
122 def controller_name
123 "#{derived_controller_name(options).split('/').last}"
124 end
125 }
126
127 @controller.send :forget_variables_added_to_assigns
128 @controller.send :render, options
129 @controller.send :process_cleanup
130 end
131
132 # This provides the template. Use this to set mock
133 # expectations for dealing with partials
134 #
135 # == Example
136 #
137 # describe "/person/new" do
138 # it "should use the form partial" do
139 # template.should_receive(:render).with(:partial => 'form')
140 # render "/person/new"
141 # end
142 # end
143 def template
144 @controller.template
145 end
146
147 Spec::Example::ExampleGroupFactory.register(:view, self)
148 end
149
150 class ViewExampleGroupController < ApplicationController #:nodoc:
151 include Spec::Rails::Example::RenderObserver
152 attr_reader :template
153
154 def add_helper_for(template_path)
155 add_helper(template_path.split('/')[0])
156 end
157
158 def add_helper(name)
159 begin
160 helper_module = "#{name}_helper".camelize.constantize
161 rescue
162 return
163 end
164 template.metaclass.class_eval do
165 include helper_module
166 end
167 end
168 end
169 end
170 end
171end