/
view_example_group.rb
149 lines (129 loc) · 4.03 KB
/
view_example_group.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
require 'rspec/rails/view_assigns'
module RSpec::Rails
# Extends ActionView::TestCase::Behavior
#
# == Examples
#
# describe "widgets/index.html.erb" do
# it "renders the @widgets" do
# widgets = [
# stub_model(Widget, :name => "Foo"),
# stub_model(Widget, :name => "Bar")
# ]
# assign(:widgets, widgets)
# render
# rendered.should contain("Foo")
# rendered.should contain("Bar")
# end
# end
module ViewExampleGroup
extend ActiveSupport::Concern
extend RSpec::Rails::ModuleInclusion
include RSpec::Rails::RailsExampleGroup
include ActionView::TestCase::Behavior
include RSpec::Rails::ViewAssigns
include RSpec::Rails::Matchers::RenderTemplate
include RSpec::Rails::BrowserSimulators
webrat do
include Webrat::Matchers
end
module ClassMethods
def _default_helper
base = metadata[:behaviour][:description].split('/').first
(base.camelize + 'Helper').constantize if base
rescue NameError
nil
end
def _default_helpers
helpers = [_default_helper].compact
helpers << ApplicationHelper if Object.const_defined?('ApplicationHelper')
helpers
end
end
module InstanceMethods
# :call-seq:
# render
# render(:template => "widgets/new.html.erb")
# render({:partial => "widgets/widget.html.erb"}, {... locals ...})
# render({:partial => "widgets/widget.html.erb"}, {... locals ...}) do ... end
#
# Delegates to ActionView::Base#render, so see documentation on that for more
# info.
#
# The only addition is that you can call render with no arguments, and RSpec
# will pass the top level description to render:
#
# describe "widgets/new.html.erb" do
# it "shows all the widgets" do
# render # => view.render(:file => "widgets/new.html.erb")
# ...
# end
# end
def render(options={}, local_assigns={}, &block)
options = {:template => _default_file_to_render} if Hash === options and options.empty?
super(options, local_assigns, &block)
end
# The instance of ActionView::Base that is used to render the template.
# Use this before the +render+ call to stub any methods you want to stub
# on the view:
#
# describe "widgets/new.html.erb" do
# it "shows all the widgets" do
# view.stub(:foo) { "foo" }
# render
# ...
# end
# end
def view
_view
end
# Provides access to the params hash that will be available within the
# view:
#
# params[:foo] = 'bar'
def params
controller.params
end
# Deprecated. Use +view+ instead.
def template
RSpec.deprecate("template","view")
view
end
# Deprecated. Use +rendered+ instead.
def response
RSpec.deprecate("response", "rendered")
rendered
end
private
def _default_file_to_render
example.example_group.top_level_description
end
def _path_parts
_default_file_to_render.split("/")
end
def _controller_path
_path_parts[0..-2].join("/")
end
def _inferred_action
_path_parts.last.split(".").first
end
def _include_controller_helpers
helpers = controller._helpers
view.singleton_class.class_eval do
include helpers unless included_modules.include?(helpers)
end
end
end
included do
metadata[:type] = :view
helper *_default_helpers
before do
_include_controller_helpers
controller.controller_path = _controller_path
controller.request.path_parameters["controller"] = _controller_path
controller.request.path_parameters["action"] = _inferred_action unless _inferred_action =~ /^_/
end
end
RSpec.configure &include_self_when_dir_matches('spec','views')
end
end