Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 115 lines (100 sloc) 3.537 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
ae48e7b @dchelimsky words
dchelimsky authored
4 # Extends ActionView::TestCase::Behavior
5 #
6 # == Examples
7 #
8 # describe "widgets/index.html.erb" do
9 # it "renders the @widgets" do
10 # widgets = [
11 # stub_model(Widget, :name => "Foo"),
12 # stub_model(Widget, :name => "Bar")
13 # ]
14 # assign(:widgets, widgets)
15 # render
16 # rendered.should contain("Foo")
17 # rendered.should contain("Bar")
18 # end
19 # end
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
20 module ViewExampleGroup
9910fed @dchelimsky Ensure that modules for different kind of specs get included in specs in
dchelimsky authored
21 extend ActiveSupport::Concern
22 extend RSpec::Rails::ModuleInclusion
c2a0fcb @dchelimsky follow some changes in rails edge
dchelimsky authored
23
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
24 include RSpec::Rails::SetupAndTeardownAdapter
25 include RSpec::Rails::TestUnitAssertionAdapter
26 include ActionView::TestCase::Behavior
27 include RSpec::Rails::ViewAssigns
28 include Webrat::Matchers
3def656 @dchelimsky expose render_template to view specs
dchelimsky authored
29 include RSpec::Rails::Matchers::RenderTemplate
8b32de7 @dchelimsky words
dchelimsky authored
30
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
31 module InstanceMethods
e86d5f5 @dchelimsky :call-seq:
dchelimsky authored
32 # :call-seq:
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
33 # render
34 # render(:template => "widgets/new.html.erb")
35 # render({:partial => "widgets/widget.html.erb"}, {... locals ...})
36 # render({:partial => "widgets/widget.html.erb"}, {... locals ...}) do ... end
37 #
38 # Delegates to ActionView::Base#render, so see documentation on that for more
39 # info.
40 #
41 # The only addition is that you can call render with no arguments, and RSpec
42 # will pass the top level description to render:
43 #
44 # describe "widgets/new.html.erb" do
45 # it "shows all the widgets" do
46 # render # => view.render(:file => "widgets/new.html.erb")
47 # ...
48 # end
49 # end
50 def render(options={}, local_assigns={}, &block)
4835b57 @dchelimsky Temporary workaround for issue #91. Last-minute assign of assigns in
dchelimsky authored
51 # TODO - this is a temporary hack to achieve behaviour that is in rails edge
52 # as of http://github.com/rails/rails/commit/0e0df4b0c5df7fdd1daa5653c255c4737f5526fc,
53 # but is not part of the rails-3.0.0.beta4 release. This line can be removed as
54 # soon as either rails 3 beta5 or rc is released.
55 _assigns.each { |key, value| view.instance_variable_set("@#{key}", value) }
56
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
57 options = {:template => _default_file_to_render} if Hash === options and options.empty?
58 super(options, local_assigns, &block)
59 end
60
f25472e @dchelimsky move instance methods into InstanceMethods in modules that extend
dchelimsky authored
61 # The instance of ActionView::Base that is used to render the template.
62 # Use this before the +render+ call to stub any methods you want to stub
63 # on the view:
64 #
65 # describe "widgets/new.html.erb" do
66 # it "shows all the widgets" do
67 # view.stub(:foo) { "foo" }
68 # render
69 # ...
70 # end
71 # end
b7184f6 @dchelimsky Reinstate view() method in view specs to provide a place to stub methods
dchelimsky authored
72 def view
73 _view
74 end
75
5f66746 @dchelimsky rdoc for last commit
dchelimsky authored
76 # Deprecated. Use +view+ instead.
d311644 @dchelimsky add deprecated template method in view spec
dchelimsky authored
77 def template
78 RSpec.deprecate("template","view")
79 view
80 end
81
759b953 @dchelimsky move some things around
dchelimsky authored
82
83 # Deprecated. Use +rendered+ instead.
84 def response
85 RSpec.deprecate("response", "rendered")
86 rendered
87 end
88
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
89 private
1e6ab79 @dchelimsky add some RSpec code examples for render() in view specs
dchelimsky authored
90
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
91 def _default_file_to_render
f3de931 @wincent Rename running_example to example to keep in sync with the rename in rsp...
wincent authored
92 example.example_group.top_level_description
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
93 end
94
95 def _controller_path
96 _default_file_to_render.split("/")[0..-2].join("/")
97 end
0e860f5 @dchelimsky Update view specs to work with ActionView::TestCase::Behavior
dchelimsky authored
98 end
8b32de7 @dchelimsky words
dchelimsky authored
99
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
100 included do
b3ab2bc @dchelimsky Add :type => :xxx to metadata for each specialized example group.
dchelimsky authored
101 metadata[:type] = :view
102
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
103 before do
104 controller.controller_path = _controller_path
3def656 @dchelimsky expose render_template to view specs
dchelimsky authored
105 # this won't be necessary if/when
106 # https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4903
107 # is accepted and released
108 @request ||= controller.request
a6a35a6 @dchelimsky move top-level modules to RSpec::Rails namespace
dchelimsky authored
109 end
8ac8cbe @dchelimsky got generated view specs passing
dchelimsky authored
110 end
111
9910fed @dchelimsky Ensure that modules for different kind of specs get included in specs in
dchelimsky authored
112 RSpec.configure &include_self_when_dir_matches('spec','views')
8b32de7 @dchelimsky words
dchelimsky authored
113 end
114 end
Something went wrong with that request. Please try again.