Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 143 lines (123 sloc) 4.179 kb
e65a2e6 @dchelimsky add helper specs, based on ActionView::TestCase::Behavior
dchelimsky authored
1 require 'rspec/rails/view_assigns'
8b32de7 @dchelimsky words
dchelimsky authored
2
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
3 module RSpec::Rails
4 module ViewExampleGroup
9910fed @dchelimsky Ensure that modules for different kind of specs get included in specs in
dchelimsky authored
5 extend ActiveSupport::Concern
fc5cdbb @dchelimsky Include rails-specific example group extensions based on :type key.
dchelimsky authored
6 include RSpec::Rails::RailsExampleGroup
bacf868 @dchelimsky include RSpec::Rails::RailsExampleGroup before other modules that need i...
dchelimsky authored
7 include ActionView::TestCase::Behavior
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
8 include RSpec::Rails::ViewAssigns
3def656 @dchelimsky expose render_template to view specs
dchelimsky authored
9 include RSpec::Rails::Matchers::RenderTemplate
d4ff873 @justinko Initial support for choosing webrat or capybara
justinko authored
10
a4200ef @wincent Make view helpers available in view specs
wincent authored
11 module ClassMethods
12 def _default_helper
c00355f @dolzenko Ensure namespaced helpers are included automatically
dolzenko authored
13 base = metadata[:example_group][:description].split('/')[0..-2].join('/')
a4200ef @wincent Make view helpers available in view specs
wincent authored
14 (base.camelize + 'Helper').constantize if base
15 rescue NameError
16 nil
17 end
18
19 def _default_helpers
20 helpers = [_default_helper].compact
21 helpers << ApplicationHelper if Object.const_defined?('ApplicationHelper')
22 helpers
23 end
24 end
25
2085462 @dchelimsky Move instance methods from InstanceMethods modules to stop deprecation
dchelimsky authored
26 module ExampleMethods
6ab0bb6 @dchelimsky cleanup and add some rdoc
dchelimsky authored
27 # @overload render
28 # @overload render({:partial => path_to_file})
29 # @overload render({:partial => path_to_file}, {... locals ...})
30 # @overload render({:partial => path_to_file}, {... locals ...}) do ... end
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
31 #
6ab0bb6 @dchelimsky cleanup and add some rdoc
dchelimsky authored
32 # Delegates to ActionView::Base#render, so see documentation on that
33 # for more info.
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
34 #
35 # The only addition is that you can call render with no arguments, and RSpec
36 # will pass the top level description to render:
37 #
c1f0eab @dchelimsky more rdoc cleanup
dchelimsky authored
38 # describe "widgets/new.html.erb" do
39 # it "shows all the widgets" do
40 # render # => view.render(:file => "widgets/new.html.erb")
41 # # ...
42 # end
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
43 # end
44 def render(options={}, local_assigns={}, &block)
45 options = {:template => _default_file_to_render} if Hash === options and options.empty?
46 super(options, local_assigns, &block)
47 end
48
c1f0eab @dchelimsky more rdoc cleanup
dchelimsky authored
49 # The instance of `ActionView::Base` that is used to render the template.
50 # Use this to stub methods _before_ calling `render`.
f25472e @dchelimsky move instance methods into InstanceMethods in modules that extend
dchelimsky authored
51 #
c1f0eab @dchelimsky more rdoc cleanup
dchelimsky authored
52 # describe "widgets/new.html.erb" do
53 # it "shows all the widgets" do
54 # view.stub(:foo) { "foo" }
55 # render
56 # # ...
57 # end
f25472e @dchelimsky move instance methods into InstanceMethods in modules that extend
dchelimsky authored
58 # end
b7184f6 @dchelimsky Reinstate view() method in view specs to provide a place to stub methods
dchelimsky authored
59 def view
60 _view
61 end
62
55acd0f @dchelimsky Add stub_template method for view specs.
dchelimsky authored
63 # Simulates the presence of a template on the file system by adding a
64 # Rails' FixtureResolver to the front of the view_paths list. Designed to
65 # help isolate view examples from partials rendered by the view template
66 # that is the subject of the example.
67 #
c1f0eab @dchelimsky more rdoc cleanup
dchelimsky authored
68 # @example
55acd0f @dchelimsky Add stub_template method for view specs.
dchelimsky authored
69 #
c1f0eab @dchelimsky more rdoc cleanup
dchelimsky authored
70 # stub_template("widgets/_widget.html.erb" => "This content.")
55acd0f @dchelimsky Add stub_template method for view specs.
dchelimsky authored
71 def stub_template(hash)
72 view.view_paths.unshift(ActionView::FixtureResolver.new(hash))
73 end
74
7b8fe1f @wincent Delegate "params" to controller in view specs
wincent authored
75 # Provides access to the params hash that will be available within the
76 # view:
77 #
c1f0eab @dchelimsky more rdoc cleanup
dchelimsky authored
78 # params[:foo] = 'bar'
7b8fe1f @wincent Delegate "params" to controller in view specs
wincent authored
79 def params
80 controller.params
81 end
82
0e6b185 @dchelimsky rdoc
dchelimsky authored
83 # @deprecated Use `view` instead.
d311644 @dchelimsky add deprecated template method in view spec
dchelimsky authored
84 def template
85 RSpec.deprecate("template","view")
86 view
87 end
88
0e6b185 @dchelimsky rdoc
dchelimsky authored
89 # @deprecated Use `rendered` instead.
759b953 @dchelimsky move some things around
dchelimsky authored
90 def response
91 RSpec.deprecate("response", "rendered")
92 rendered
93 end
94
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
95 private
1e6ab79 @dchelimsky add some RSpec code examples for render() in view specs
dchelimsky authored
96
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
97 def _default_file_to_render
f3de931 @wincent Rename running_example to example to keep in sync with the rename in rsp...
wincent authored
98 example.example_group.top_level_description
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
99 end
100
a28fd37 @dchelimsky Infer the controller action from the view path
dchelimsky authored
101 def _path_parts
102 _default_file_to_render.split("/")
103 end
104
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
105 def _controller_path
a28fd37 @dchelimsky Infer the controller action from the view path
dchelimsky authored
106 _path_parts[0..-2].join("/")
107 end
108
109 def _inferred_action
110 _path_parts.last.split(".").first
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
111 end
13b8013 @dchelimsky Include helper methods defined in controllers in the view object in
dchelimsky authored
112
113 def _include_controller_helpers
114 helpers = controller._helpers
115 view.singleton_class.class_eval do
116 include helpers unless included_modules.include?(helpers)
117 end
118 end
0e860f5 @dchelimsky Update view specs to work with ActionView::TestCase::Behavior
dchelimsky authored
119 end
8b32de7 @dchelimsky words
dchelimsky authored
120
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
121 included do
2085462 @dchelimsky Move instance methods from InstanceMethods modules to stop deprecation
dchelimsky authored
122 include ExampleMethods
123
b3ab2bc @dchelimsky Add :type => :xxx to metadata for each specialized example group.
dchelimsky authored
124 metadata[:type] = :view
51893e6 @dchelimsky Funny thing, but turning on warnings produces a bunch of warnings. Mostl...
dchelimsky authored
125 helper(*_default_helpers)
b3ab2bc @dchelimsky Add :type => :xxx to metadata for each specialized example group.
dchelimsky authored
126
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
127 before do
13b8013 @dchelimsky Include helper methods defined in controllers in the view object in
dchelimsky authored
128 _include_controller_helpers
4597ecd @dchelimsky Support controller specs with both Rails 3.0 and 3.1
dchelimsky authored
129 if view.lookup_context.respond_to?(:prefixes)
130 # rails 3.1
131 view.lookup_context.prefixes << _controller_path
132 else
133 # rails 3.0
134 controller.controller_path = _controller_path
135 end
732a922 @dchelimsky Infer controller.request.path_params["controller"] from the file being
dchelimsky authored
136 controller.request.path_parameters["controller"] = _controller_path
a28fd37 @dchelimsky Infer the controller action from the view path
dchelimsky authored
137 controller.request.path_parameters["action"] = _inferred_action unless _inferred_action =~ /^_/
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
138 end
8ac8cbe @dchelimsky got generated view specs passing
dchelimsky authored
139 end
8b32de7 @dchelimsky words
dchelimsky authored
140 end
141 end
a28fd37 @dchelimsky Infer the controller action from the view path
dchelimsky authored
142
Something went wrong with that request. Please try again.