rspec-rails 2.0.0.beta.22 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. data/.gitignore +10 -4
  2. data/.rspec +1 -0
  3. data/.travis.yml +10 -0
  4. data/History.md +1 -33
  5. data/License.txt +23 -0
  6. data/{README.markdown → README.md} +27 -7
  7. data/README_DEV.md +43 -0
  8. data/Rakefile +105 -35
  9. data/Thorfile +40 -0
  10. data/Upgrade.md +1 -0
  11. data/features/.nav +34 -0
  12. data/features/Autotest.md +24 -0
  13. data/features/Changelog.md +151 -0
  14. data/features/Generators.md +8 -0
  15. data/features/GettingStarted.md +84 -0
  16. data/features/README.md +58 -0
  17. data/features/Upgrade.md +117 -0
  18. data/features/controller_specs/README.md +45 -0
  19. data/features/controller_specs/anonymous_controller.feature +7 -7
  20. data/features/controller_specs/controller_spec.feature +18 -0
  21. data/features/controller_specs/isolation_from_views.feature +55 -8
  22. data/features/controller_specs/render_views.feature +63 -52
  23. data/features/helper_specs/helper_spec.feature +40 -6
  24. data/features/mailer_specs/url_helpers.feature +4 -4
  25. data/features/matchers/README.md +4 -0
  26. data/features/matchers/new_record_matcher.feature +6 -48
  27. data/features/matchers/redirect_to_matcher.feature +5 -6
  28. data/features/matchers/render_template_matcher.feature +13 -10
  29. data/features/mocks/mock_model.feature +29 -13
  30. data/features/mocks/stub_model.feature +4 -4
  31. data/features/model_specs/README.md +21 -0
  32. data/features/model_specs/errors_on.feature +2 -2
  33. data/features/model_specs/transactional_examples.feature +14 -9
  34. data/features/request_specs/request_spec.feature +49 -0
  35. data/features/routing_specs/README.md +17 -0
  36. data/features/{matchers → routing_specs}/be_routable_matcher.feature +25 -25
  37. data/features/routing_specs/named_routes.feature +18 -0
  38. data/features/routing_specs/route_to_matcher.feature +58 -0
  39. data/features/step_definitions/additional_cli_steps.rb +4 -0
  40. data/features/support/env.rb +14 -6
  41. data/features/view_specs/inferred_controller_path.feature +6 -6
  42. data/features/view_specs/stub_template.feature +51 -0
  43. data/features/view_specs/view_spec.feature +23 -42
  44. data/gemfiles/.bundle/config +2 -0
  45. data/gemfiles/base.rb +58 -0
  46. data/gemfiles/rails-3-0-stable +6 -0
  47. data/gemfiles/rails-3.0.0 +5 -0
  48. data/gemfiles/rails-3.0.1 +5 -0
  49. data/gemfiles/rails-3.0.2 +5 -0
  50. data/gemfiles/rails-3.0.3 +5 -0
  51. data/gemfiles/rails-3.0.4 +5 -0
  52. data/gemfiles/rails-3.0.5 +5 -0
  53. data/gemfiles/rails-3.0.6 +5 -0
  54. data/gemfiles/rails-3.0.7 +5 -0
  55. data/gemfiles/rails-3.1.0.beta1 +5 -0
  56. data/gemfiles/rails-master +7 -0
  57. data/lib/autotest/rails_rspec2.rb +1 -1
  58. data/lib/generators/rspec/controller/controller_generator.rb +4 -4
  59. data/lib/generators/rspec/helper/helper_generator.rb +3 -3
  60. data/lib/generators/rspec/install/install_generator.rb +0 -8
  61. data/lib/generators/rspec/integration/integration_generator.rb +15 -1
  62. data/lib/generators/rspec/integration/templates/request_spec.rb +7 -1
  63. data/lib/generators/rspec/mailer/mailer_generator.rb +2 -2
  64. data/lib/generators/rspec/model/model_generator.rb +1 -1
  65. data/lib/generators/rspec/observer/observer_generator.rb +1 -1
  66. data/lib/generators/rspec/scaffold/scaffold_generator.rb +40 -11
  67. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +90 -58
  68. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +15 -7
  69. data/lib/generators/rspec/scaffold/templates/index_spec.rb +7 -2
  70. data/lib/generators/rspec/scaffold/templates/new_spec.rb +14 -7
  71. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +14 -14
  72. data/lib/generators/rspec/scaffold/templates/show_spec.rb +7 -2
  73. data/lib/generators/rspec/view/view_generator.rb +1 -1
  74. data/lib/rspec/rails/example/controller_example_group.rb +25 -34
  75. data/lib/rspec/rails/example/helper_example_group.rb +5 -11
  76. data/lib/rspec/rails/example/mailer_example_group.rb +0 -14
  77. data/lib/rspec/rails/example/model_example_group.rb +0 -4
  78. data/lib/rspec/rails/example/rails_example_group.rb +0 -2
  79. data/lib/rspec/rails/example/request_example_group.rb +5 -30
  80. data/lib/rspec/rails/example/routing_example_group.rb +3 -5
  81. data/lib/rspec/rails/example/view_example_group.rb +20 -11
  82. data/lib/rspec/rails/example.rb +31 -1
  83. data/lib/rspec/rails/extensions/active_record/base.rb +28 -32
  84. data/lib/rspec/rails/fixture_support.rb +1 -1
  85. data/lib/rspec/rails/matchers/be_new_record.rb +5 -0
  86. data/lib/rspec/rails/matchers/redirect_to.rb +6 -2
  87. data/lib/rspec/rails/matchers/render_template.rb +5 -1
  88. data/lib/rspec/rails/matchers/routing_matchers.rb +24 -3
  89. data/lib/rspec/rails/matchers.rb +1 -1
  90. data/lib/rspec/rails/mocks.rb +118 -79
  91. data/lib/rspec/rails/module_inclusion.rb +17 -0
  92. data/lib/rspec/rails/tasks/rspec.rake +1 -1
  93. data/lib/rspec/rails/vendor/capybara.rb +29 -0
  94. data/lib/rspec/rails/vendor/webrat.rb +33 -0
  95. data/lib/rspec/rails/version.rb +1 -1
  96. data/lib/rspec/rails/view_assigns.rb +23 -4
  97. data/lib/rspec/rails/view_rendering.rb +50 -13
  98. data/lib/rspec/rails.rb +7 -10
  99. data/lib/rspec-rails.rb +4 -2
  100. data/rspec-rails.gemspec +11 -39
  101. data/spec/autotest/rails_rspec2_spec.rb +25 -14
  102. data/spec/rspec/rails/configuration_spec.rb +26 -0
  103. data/spec/rspec/rails/deprecations_spec.rb +18 -0
  104. data/spec/rspec/rails/example/controller_example_group_spec.rb +47 -5
  105. data/spec/rspec/rails/example/helper_example_group_spec.rb +19 -1
  106. data/spec/rspec/rails/example/request_example_group_spec.rb +2 -0
  107. data/spec/rspec/rails/extensions/active_model/errors_on_spec.rb +23 -0
  108. data/spec/rspec/rails/extensions/active_record/records_spec.rb +9 -0
  109. data/spec/rspec/rails/matchers/be_new_record_spec.rb +17 -0
  110. data/spec/rspec/rails/matchers/be_routable_spec.rb +41 -0
  111. data/spec/rspec/rails/matchers/redirect_to_spec.rb +67 -8
  112. data/spec/rspec/rails/matchers/render_template_spec.rb +67 -10
  113. data/spec/rspec/rails/matchers/route_to_spec.rb +77 -31
  114. data/spec/rspec/rails/mocks/ar_classes.rb +17 -25
  115. data/spec/rspec/rails/mocks/mock_model_spec.rb +50 -13
  116. data/spec/rspec/rails/mocks/stub_model_spec.rb +121 -67
  117. data/spec/rspec/rails/view_rendering_spec.rb +105 -0
  118. data/spec/spec_helper.rb +0 -22
  119. data/spec/support/matchers.rb +9 -0
  120. data/templates/generate_stuff.rb +10 -2
  121. data/templates/run_specs.rb +2 -2
  122. metadata +141 -79
  123. data/Gemfile +0 -24
  124. data/Gotchas.markdown +0 -14
  125. data/Upgrade.markdown +0 -31
  126. data/autotest/discover.rb +0 -1
  127. data/features/README.markdown +0 -12
  128. data/features/controller_specs/readers.feature +0 -18
  129. data/features/routing_specs/access_to_named_routes.feature +0 -15
  130. data/lib/generators/rspec/install/templates/autotest/discover.rb +0 -2
  131. data/lib/rspec/rails/browser_simulators.rb +0 -30
  132. data/spec/rspec/rails/example/view_rendering_spec.rb +0 -110
  133. data/specs.watchr +0 -59
  134. data/templates/Gemfile +0 -18
@@ -1,126 +1,158 @@
1
1
  require 'spec_helper'
2
2
 
3
+ # This spec was generated by rspec-rails when you ran the scaffold generator.
4
+ # It demonstrates how one might use RSpec to specify the controller code that
5
+ # was generated by Rails when you ran the scaffold generator.
6
+ #
7
+ # It assumes that the implementation code is generated by the rails scaffold
8
+ # generator. If you are using any extension libraries to generate different
9
+ # controller code, this generated spec may or may not pass.
10
+ #
11
+ # It only uses APIs available in rails and/or rspec-rails. There are a number
12
+ # of tools you can use to make these specs even more expressive, but we're
13
+ # sticking to rails and rspec-rails APIs to keep things simple and stable.
14
+ #
15
+ # Compared to earlier versions of this generator, there is very limited use of
16
+ # stubs and message expectations in this spec. Stubs are only used when there
17
+ # is no simpler way to get a handle on the object needed for the example.
18
+ # Message expectations are only used when there is no simpler way to specify
19
+ # that an instance is receiving a specific message.
20
+
3
21
  describe <%= controller_class_name %>Controller do
4
22
 
5
- def <%= mock_file_name %>(stubs={})
6
- @<%= mock_file_name %> ||= mock_model(<%= class_name %>, stubs).as_null_object
23
+ # This should return the minimal set of attributes required to create a valid
24
+ # <%= class_name %>. As you add validations to <%= class_name %>, be sure to
25
+ # update the return value of this method accordingly.
26
+ def valid_attributes
27
+ {}
7
28
  end
8
29
 
9
30
  <% unless options[:singleton] -%>
10
31
  describe "GET index" do
11
32
  it "assigns all <%= table_name.pluralize %> as @<%= table_name.pluralize %>" do
12
- <%= stub! orm_class.all(class_name) %> { [<%= mock_file_name %>] }
33
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
13
34
  get :index
14
- assigns(:<%= table_name %>).should eq([<%= mock_file_name %>])
35
+ assigns(:<%= table_name %>).should eq([<%= file_name %>])
15
36
  end
16
37
  end
17
38
 
18
39
  <% end -%>
19
40
  describe "GET show" do
20
- it "assigns the requested <%= file_name %> as @<%= file_name %>" do
21
- <%= stub! orm_class.find(class_name, "37".inspect) %> { <%= mock_file_name %> }
22
- get :show, :id => "37"
23
- assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
41
+ it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
42
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
43
+ get :show, :id => <%= file_name %>.id.to_s
44
+ assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
24
45
  end
25
46
  end
26
47
 
27
48
  describe "GET new" do
28
- it "assigns a new <%= file_name %> as @<%= file_name %>" do
29
- <%= stub! orm_class.build(class_name) %> { <%= mock_file_name %> }
49
+ it "assigns a new <%= ns_file_name %> as @<%= ns_file_name %>" do
30
50
  get :new
31
- assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
51
+ assigns(:<%= ns_file_name %>).should be_a_new(<%= class_name %>)
32
52
  end
33
53
  end
34
54
 
35
55
  describe "GET edit" do
36
- it "assigns the requested <%= file_name %> as @<%= file_name %>" do
37
- <%= stub! orm_class.find(class_name, "37".inspect) %> { <%= mock_file_name %> }
38
- get :edit, :id => "37"
39
- assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
56
+ it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
57
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
58
+ get :edit, :id => <%= file_name %>.id.to_s
59
+ assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
40
60
  end
41
61
  end
42
62
 
43
63
  describe "POST create" do
44
-
45
64
  describe "with valid params" do
46
- it "assigns a newly created <%= file_name %> as @<%= file_name %>" do
47
- <%= stub! orm_class.build(class_name, params) %> { <%= mock_file_name(:save => true) %> }
48
- post :create, :<%= file_name %> => <%= params %>
49
- assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
65
+ it "creates a new <%= class_name %>" do
66
+ expect {
67
+ post :create, :<%= ns_file_name %> => valid_attributes
68
+ }.to change(<%= class_name %>, :count).by(1)
69
+ end
70
+
71
+ it "assigns a newly created <%= ns_file_name %> as @<%= ns_file_name %>" do
72
+ post :create, :<%= ns_file_name %> => valid_attributes
73
+ assigns(:<%= ns_file_name %>).should be_a(<%= class_name %>)
74
+ assigns(:<%= ns_file_name %>).should be_persisted
50
75
  end
51
76
 
52
- it "redirects to the created <%= file_name %>" do
53
- <%= stub! orm_class.build(class_name) %> { <%= mock_file_name(:save => true) %> }
54
- post :create, :<%= file_name %> => {}
55
- response.should redirect_to(<%= table_name.singularize %>_url(<%= mock_file_name %>))
77
+ it "redirects to the created <%= ns_file_name %>" do
78
+ post :create, :<%= ns_file_name %> => valid_attributes
79
+ response.should redirect_to(<%= class_name %>.last)
56
80
  end
57
81
  end
58
82
 
59
83
  describe "with invalid params" do
60
- it "assigns a newly created but unsaved <%= file_name %> as @<%= file_name %>" do
61
- <%= stub! orm_class.build(class_name, params) %> { <%= mock_file_name(:save => false) %> }
62
- post :create, :<%= file_name %> => <%= params %>
63
- assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
84
+ it "assigns a newly created but unsaved <%= ns_file_name %> as @<%= ns_file_name %>" do
85
+ # Trigger the behavior that occurs when invalid params are submitted
86
+ <%= class_name %>.any_instance.stub(:save).and_return(false)
87
+ post :create, :<%= ns_file_name %> => {}
88
+ assigns(:<%= ns_file_name %>).should be_a_new(<%= class_name %>)
64
89
  end
65
90
 
66
91
  it "re-renders the 'new' template" do
67
- <%= stub! orm_class.build(class_name) %> { <%= mock_file_name(:save => false) %> }
68
- post :create, :<%= file_name %> => {}
92
+ # Trigger the behavior that occurs when invalid params are submitted
93
+ <%= class_name %>.any_instance.stub(:save).and_return(false)
94
+ post :create, :<%= ns_file_name %> => {}
69
95
  response.should render_template("new")
70
96
  end
71
97
  end
72
-
73
98
  end
74
99
 
75
100
  describe "PUT update" do
76
-
77
101
  describe "with valid params" do
78
- it "updates the requested <%= file_name %>" do
79
- <%= should_receive! orm_class.find(class_name, "37".inspect) %> { <%= mock_file_name %> }
80
- mock_<%= should_receive! orm_instance.update_attributes(params) %>
81
- put :update, :id => "37", :<%= file_name %> => <%= params %>
102
+ it "updates the requested <%= ns_file_name %>" do
103
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
104
+ # Assuming there are no other <%= table_name %> in the database, this
105
+ # specifies that the <%= class_name %> created on the previous line
106
+ # receives the :update_attributes message with whatever params are
107
+ # submitted in the request.
108
+ <%= class_name %>.any_instance.should_receive(:update_attributes).with(<%= params %>)
109
+ put :update, :id => <%= file_name %>.id, :<%= ns_file_name %> => <%= params %>
82
110
  end
83
111
 
84
- it "assigns the requested <%= file_name %> as @<%= file_name %>" do
85
- <%= stub! orm_class.find(class_name) %> { <%= mock_file_name(:update_attributes => true) %> }
86
- put :update, :id => "1"
87
- assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
112
+ it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
113
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
114
+ put :update, :id => <%= file_name %>.id, :<%= ns_file_name %> => valid_attributes
115
+ assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
88
116
  end
89
117
 
90
- it "redirects to the <%= file_name %>" do
91
- <%= stub! orm_class.find(class_name) %> { <%= mock_file_name(:update_attributes => true) %> }
92
- put :update, :id => "1"
93
- response.should redirect_to(<%= table_name.singularize %>_url(<%= mock_file_name %>))
118
+ it "redirects to the <%= ns_file_name %>" do
119
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
120
+ put :update, :id => <%= file_name %>.id, :<%= ns_file_name %> => valid_attributes
121
+ response.should redirect_to(<%= file_name %>)
94
122
  end
95
123
  end
96
124
 
97
125
  describe "with invalid params" do
98
- it "assigns the <%= file_name %> as @<%= file_name %>" do
99
- <%= stub! orm_class.find(class_name) %> { <%= mock_file_name(:update_attributes => false) %> }
100
- put :update, :id => "1"
101
- assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
126
+ it "assigns the <%= ns_file_name %> as @<%= ns_file_name %>" do
127
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
128
+ # Trigger the behavior that occurs when invalid params are submitted
129
+ <%= class_name %>.any_instance.stub(:save).and_return(false)
130
+ put :update, :id => <%= file_name %>.id.to_s, :<%= ns_file_name %> => {}
131
+ assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
102
132
  end
103
133
 
104
134
  it "re-renders the 'edit' template" do
105
- <%= stub! orm_class.find(class_name) %> { <%= mock_file_name(:update_attributes => false) %> }
106
- put :update, :id => "1"
135
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
136
+ # Trigger the behavior that occurs when invalid params are submitted
137
+ <%= class_name %>.any_instance.stub(:save).and_return(false)
138
+ put :update, :id => <%= file_name %>.id.to_s, :<%= ns_file_name %> => {}
107
139
  response.should render_template("edit")
108
140
  end
109
141
  end
110
-
111
142
  end
112
143
 
113
144
  describe "DELETE destroy" do
114
- it "destroys the requested <%= file_name %>" do
115
- <%= should_receive! orm_class.find(class_name, "37".inspect) %> { <%= mock_file_name %> }
116
- mock_<%= should_receive! orm_instance.destroy %>
117
- delete :destroy, :id => "37"
145
+ it "destroys the requested <%= ns_file_name %>" do
146
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
147
+ expect {
148
+ delete :destroy, :id => <%= file_name %>.id.to_s
149
+ }.to change(<%= class_name %>, :count).by(-1)
118
150
  end
119
151
 
120
152
  it "redirects to the <%= table_name %> list" do
121
- <%= stub! orm_class.find(class_name) %> { <%= mock_file_name %> }
122
- delete :destroy, :id => "1"
123
- response.should redirect_to(<%= table_name %>_url)
153
+ <%= file_name %> = <%= class_name %>.create! valid_attributes
154
+ delete :destroy, :id => <%= file_name %>.id.to_s
155
+ response.should redirect_to(<%= index_helper %>_url)
124
156
  end
125
157
  end
126
158
 
@@ -1,23 +1,31 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- describe "<%= table_name %>/edit.html.<%= options[:template_engine] %>" do
4
+ describe "<%= ns_table_name %>/edit.html.<%= options[:template_engine] %>" do
5
5
  before(:each) do
6
- @<%= file_name %> = assign(:<%= file_name %>, stub_model(<%= class_name %>,
7
- :new_record? => false<%= output_attributes.empty? ? '' : ',' %>
6
+ @<%= ns_file_name %> = assign(:<%= ns_file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? '))' : ',' %>
8
7
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
9
8
  :<%= attribute.name %> => <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
10
9
  <% end -%>
11
- ))
10
+ <%= output_attributes.empty? ? "" : " ))\n" -%>
12
11
  end
13
12
 
14
- it "renders the edit <%= file_name %> form" do
13
+ it "renders the edit <%= ns_file_name %> form" do
15
14
  render
16
15
 
17
- rendered.should have_selector("form", :action => <%= file_name %>_path(@<%= file_name %>), :method => "post") do |form|
16
+ <% if webrat? -%>
17
+ rendered.should have_selector("form", :action => <%= ns_file_name %>_path(@<%= ns_file_name %>), :method => "post") do |form|
18
18
  <% for attribute in output_attributes -%>
19
- form.should have_selector("<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
19
+ form.should have_selector("<%= attribute.input_type -%>#<%= ns_file_name %>_<%= attribute.name %>", :name => "<%= ns_file_name %>[<%= attribute.name %>]")
20
20
  <% end -%>
21
21
  end
22
+ <% else -%>
23
+ # Run the generator again with the --webrat flag if you want to use webrat matchers
24
+ assert_select "form", :action => <%= index_helper %>_path(@<%= ns_file_name %>), :method => "post" do
25
+ <% for attribute in output_attributes -%>
26
+ assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= attribute.name %>", :name => "<%= ns_file_name %>[<%= attribute.name %>]"
27
+ <% end -%>
28
+ end
29
+ <% end -%>
22
30
  end
23
31
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- describe "<%= table_name %>/index.html.<%= options[:template_engine] %>" do
4
+ describe "<%= ns_table_name %>/index.html.<%= options[:template_engine] %>" do
5
5
  before(:each) do
6
6
  assign(:<%= table_name %>, [
7
7
  <% [1,2].each_with_index do |id, model_index| -%>
@@ -16,10 +16,15 @@ describe "<%= table_name %>/index.html.<%= options[:template_engine] %>" do
16
16
  ])
17
17
  end
18
18
 
19
- it "renders a list of <%= table_name %>" do
19
+ it "renders a list of <%= ns_table_name %>" do
20
20
  render
21
21
  <% for attribute in output_attributes -%>
22
+ <% if webrat? -%>
22
23
  rendered.should have_selector("tr>td", :content => <%= value_for(attribute) %>.to_s, :count => 2)
24
+ <% else -%>
25
+ # Run the generator again with the --webrat flag if you want to use webrat matchers
26
+ assert_select "tr>td", :text => <%= value_for(attribute) %>.to_s, :count => 2
27
+ <% end -%>
23
28
  <% end -%>
24
29
  end
25
30
  end
@@ -1,23 +1,30 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- describe "<%= table_name %>/new.html.<%= options[:template_engine] %>" do
4
+ describe "<%= ns_table_name %>/new.html.<%= options[:template_engine] %>" do
5
5
  before(:each) do
6
- assign(:<%= file_name %>, stub_model(<%= class_name %>,
7
- :new_record? => true<%= output_attributes.empty? ? '' : ',' %>
6
+ assign(:<%= ns_file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? ').as_new_record)' : ',' %>
8
7
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
9
8
  :<%= attribute.name %> => <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
10
9
  <% end -%>
11
- ))
12
- end
10
+ <%= !output_attributes.empty? ? " ).as_new_record)\n end" : " end" %>
13
11
 
14
- it "renders new <%= file_name %> form" do
12
+ it "renders new <%= ns_file_name %> form" do
15
13
  render
16
14
 
15
+ <% if webrat? -%>
17
16
  rendered.should have_selector("form", :action => <%= table_name %>_path, :method => "post") do |form|
18
17
  <% for attribute in output_attributes -%>
19
- form.should have_selector("<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
18
+ form.should have_selector("<%= attribute.input_type -%>#<%= ns_file_name %>_<%= attribute.name %>", :name => "<%= ns_file_name %>[<%= attribute.name %>]")
20
19
  <% end -%>
21
20
  end
21
+ <% else -%>
22
+ # Run the generator again with the --webrat flag if you want to use webrat matchers
23
+ assert_select "form", :action => <%= index_helper %>_path, :method => "post" do
24
+ <% for attribute in output_attributes -%>
25
+ assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= attribute.name %>", :name => "<%= ns_file_name %>[<%= attribute.name %>]"
26
+ <% end -%>
27
+ end
28
+ <% end -%>
22
29
  end
23
30
  end
@@ -4,33 +4,33 @@ describe <%= controller_class_name %>Controller do
4
4
  describe "routing" do
5
5
 
6
6
  <% unless options[:singleton] -%>
7
- it "recognizes and generates #index" do
8
- { :get => "/<%= table_name %>" }.should route_to(:controller => "<%= table_name %>", :action => "index")
7
+ it "routes to #index" do
8
+ get("/<%= ns_table_name %>").should route_to("<%= ns_table_name %>#index")
9
9
  end
10
10
 
11
11
  <% end -%>
12
- it "recognizes and generates #new" do
13
- { :get => "/<%= table_name %>/new" }.should route_to(:controller => "<%= table_name %>", :action => "new")
12
+ it "routes to #new" do
13
+ get("/<%= ns_table_name %>/new").should route_to("<%= ns_table_name %>#new")
14
14
  end
15
15
 
16
- it "recognizes and generates #show" do
17
- { :get => "/<%= table_name %>/1" }.should route_to(:controller => "<%= table_name %>", :action => "show", :id => "1")
16
+ it "routes to #show" do
17
+ get("/<%= ns_table_name %>/1").should route_to("<%= ns_table_name %>#show", :id => "1")
18
18
  end
19
19
 
20
- it "recognizes and generates #edit" do
21
- { :get => "/<%= table_name %>/1/edit" }.should route_to(:controller => "<%= table_name %>", :action => "edit", :id => "1")
20
+ it "routes to #edit" do
21
+ get("/<%= ns_table_name %>/1/edit").should route_to("<%= ns_table_name %>#edit", :id => "1")
22
22
  end
23
23
 
24
- it "recognizes and generates #create" do
25
- { :post => "/<%= table_name %>" }.should route_to(:controller => "<%= table_name %>", :action => "create")
24
+ it "routes to #create" do
25
+ post("/<%= ns_table_name %>").should route_to("<%= ns_table_name %>#create")
26
26
  end
27
27
 
28
- it "recognizes and generates #update" do
29
- { :put => "/<%= table_name %>/1" }.should route_to(:controller => "<%= table_name %>", :action => "update", :id => "1")
28
+ it "routes to #update" do
29
+ put("/<%= ns_table_name %>/1").should route_to("<%= ns_table_name %>#update", :id => "1")
30
30
  end
31
31
 
32
- it "recognizes and generates #destroy" do
33
- { :delete => "/<%= table_name %>/1" }.should route_to(:controller => "<%= table_name %>", :action => "destroy", :id => "1")
32
+ it "routes to #destroy" do
33
+ delete("/<%= ns_table_name %>/1").should route_to("<%= ns_table_name %>#destroy", :id => "1")
34
34
  end
35
35
 
36
36
  end
@@ -1,9 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- describe "<%= table_name %>/show.html.<%= options[:template_engine] %>" do
4
+ describe "<%= ns_table_name %>/show.html.<%= options[:template_engine] %>" do
5
5
  before(:each) do
6
- @<%= file_name %> = assign(:<%= file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? '))' : ',' %>
6
+ @<%= ns_file_name %> = assign(:<%= ns_file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? '))' : ',' %>
7
7
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
8
8
  :<%= attribute.name %> => <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
9
9
  <% end -%>
@@ -15,7 +15,12 @@ describe "<%= table_name %>/show.html.<%= options[:template_engine] %>" do
15
15
  it "renders attributes in <p>" do
16
16
  render
17
17
  <% for attribute in output_attributes -%>
18
+ <% if webrat? -%>
18
19
  rendered.should contain(<%= value_for(attribute) %>.to_s)
20
+ <% else -%>
21
+ # Run the generator again with the --webrat flag if you want to use webrat matchers
22
+ rendered.should match(/<%= eval(value_for(attribute)) %>/)
23
+ <% end -%>
19
24
  <% end -%>
20
25
  end
21
26
  end
@@ -7,7 +7,7 @@ module Rspec
7
7
 
8
8
  class_option :template_engine, :desc => "Template engine to generate view files"
9
9
 
10
- def create_view_files
10
+ def create_view_specs
11
11
  empty_directory File.join("spec", "views", file_path)
12
12
 
13
13
  actions.each do |action|
@@ -56,15 +56,19 @@ module RSpec::Rails
56
56
  #
57
57
  # RSpec's preferred approach to spec'ing controller behaviour is to isolate
58
58
  # the controller from its collaborators. By default, therefore, controller
59
- # example groups do not render views. This means that a view template need
60
- # not even exist in order to run a controller spec, and you can still specify
61
- # which template the controller should render.
59
+ # example groups do not render the views in your app. Due to the way Rails
60
+ # searches for view templates, the template still needs to exist, but it
61
+ # won't actually be loaded.
62
+ #
63
+ # NOTE that this is different from rspec-rails-1 with rails-2, which did not
64
+ # require the presence of the file at all. Due to changes in rails-3, this
65
+ # was no longer feasible in rspec-rails-2.
62
66
  #
63
67
  # == View rendering
64
68
  #
65
- # If you prefer a more integrated approach, similar to that of
66
- # Rails' functional tests, you can tell controller groups to
67
- # render views with the +render_views+ declaration:
69
+ # If you prefer a more integrated approach, similar to that of Rails'
70
+ # functional tests, you can tell controller groups to render the views in the
71
+ # app with the +render_views+ declaration:
68
72
  #
69
73
  # describe WidgetsController do
70
74
  # render_views
@@ -72,32 +76,12 @@ module RSpec::Rails
72
76
  #
73
77
  module ControllerExampleGroup
74
78
  extend ActiveSupport::Concern
75
- extend RSpec::Rails::ModuleInclusion
76
-
77
79
  include RSpec::Rails::RailsExampleGroup
78
-
79
80
  include ActionController::TestCase::Behavior
80
81
  include RSpec::Rails::ViewRendering
81
82
  include RSpec::Rails::Matchers::RedirectTo
82
83
  include RSpec::Rails::Matchers::RenderTemplate
83
84
  include RSpec::Rails::Matchers::RoutingMatchers
84
- include RSpec::Rails::BrowserSimulators
85
-
86
- webrat do
87
- include Webrat::Methods
88
- include Webrat::Matchers
89
- end
90
-
91
- capybara do
92
- include Capybara
93
- end
94
-
95
- # TODO (DC 7/31/2010) this is already included in RailsExampleGroup, but
96
- # due to some load order dependency problem between Webrat::Matchers and
97
- # RSpec::Matchers, combined with the fact that RailsExampleGroup extends
98
- # ActiveSupport::Concern, while the matcher modules do not, this needs to
99
- # be here as well. At least for now.
100
- include RSpec::Matchers
101
85
 
102
86
  module ClassMethods
103
87
  def controller_class
@@ -141,11 +125,9 @@ module RSpec::Rails
141
125
  # defined in +ApplicationController+, however, are accessible from within
142
126
  # the block.
143
127
  def controller(base_class = ApplicationController, &body)
144
- metadata[:example_group][:describes] = Class.new(base_class, &body)
145
- metadata[:example_group][:describes].singleton_class.class_eval do
146
- def name
147
- "StubResourcesController"
148
- end
128
+ base_class.dup.tap do |new_base|
129
+ def new_base.name; "StubResourcesController"; end
130
+ metadata[:example_group][:describes] = Class.new(new_base, &body)
149
131
  end
150
132
 
151
133
  before do
@@ -154,23 +136,32 @@ module RSpec::Rails
154
136
  end
155
137
 
156
138
  after do
157
- @routes = @orig_routes
139
+ @routes, @orig_routes = @orig_routes, nil
158
140
  end
159
141
  end
160
142
  end
161
143
 
162
144
  module InstanceMethods
163
145
  attr_reader :controller, :routes
146
+
147
+ def method_missing(method, *args, &block)
148
+ if @orig_routes && @orig_routes.named_routes.helpers.include?(method)
149
+ controller.send(method, *args, &block)
150
+ else
151
+ super
152
+ end
153
+ end
164
154
  end
165
155
 
166
156
  included do
157
+ subject { controller }
158
+
167
159
  metadata[:type] = :controller
160
+
168
161
  before do
169
162
  @routes = ::Rails.application.routes
170
163
  ActionController::Base.allow_forgery_protection = false
171
164
  end
172
165
  end
173
-
174
- RSpec.configure &include_self_when_dir_matches('spec','controllers')
175
166
  end
176
167
  end
@@ -27,17 +27,9 @@ module RSpec::Rails
27
27
  #
28
28
  module HelperExampleGroup
29
29
  extend ActiveSupport::Concern
30
- extend RSpec::Rails::ModuleInclusion
31
-
32
30
  include RSpec::Rails::RailsExampleGroup
33
-
34
31
  include ActionView::TestCase::Behavior
35
32
  include RSpec::Rails::ViewAssigns
36
- include RSpec::Rails::BrowserSimulators
37
-
38
- webrat do
39
- include Webrat::Matchers
40
- end
41
33
 
42
34
  module ClassMethods
43
35
  def determine_default_helper_class(ignore)
@@ -49,7 +41,10 @@ module RSpec::Rails
49
41
  # Returns an instance of ActionView::Base with the helper being specified
50
42
  # mixed in, along with any of the built-in rails helpers.
51
43
  def helper
52
- _view.tap {|v| v.assign(_assigns)}
44
+ _view.tap do |v|
45
+ v.extend(ApplicationHelper) if defined?(ApplicationHelper)
46
+ v.assign(view_assigns)
47
+ end
53
48
  end
54
49
 
55
50
  private
@@ -61,11 +56,10 @@ module RSpec::Rails
61
56
 
62
57
  included do
63
58
  metadata[:type] = :helper
59
+
64
60
  before do
65
61
  controller.controller_path = _controller_path
66
62
  end
67
63
  end
68
-
69
- RSpec.configure &include_self_when_dir_matches('spec','helpers')
70
64
  end
71
65
  end
@@ -2,20 +2,8 @@ if defined?(ActionMailer)
2
2
  module RSpec::Rails
3
3
  module MailerExampleGroup
4
4
  extend ActiveSupport::Concern
5
- extend RSpec::Rails::ModuleInclusion
6
-
7
5
  include RSpec::Rails::RailsExampleGroup
8
-
9
6
  include ActionMailer::TestCase::Behavior
10
- include RSpec::Rails::BrowserSimulators
11
-
12
- webrat do
13
- include Webrat::Matchers
14
- end
15
-
16
- capybara do
17
- include Capybara
18
- end
19
7
 
20
8
  included do
21
9
  metadata[:type] = :mailer
@@ -29,8 +17,6 @@ if defined?(ActionMailer)
29
17
  describes
30
18
  end
31
19
  end
32
-
33
- RSpec.configure &include_self_when_dir_matches('spec','mailers')
34
20
  end
35
21
  end
36
22
  end
@@ -1,14 +1,10 @@
1
1
  module RSpec::Rails
2
2
  module ModelExampleGroup
3
3
  extend ActiveSupport::Concern
4
- extend RSpec::Rails::ModuleInclusion
5
-
6
4
  include RSpec::Rails::RailsExampleGroup
7
5
 
8
6
  included do
9
7
  metadata[:type] = :model
10
8
  end
11
-
12
- RSpec.configure &include_self_when_dir_matches('spec','models')
13
9
  end
14
10
  end
@@ -2,10 +2,8 @@ module RSpec
2
2
  module Rails
3
3
  module RailsExampleGroup
4
4
  extend ActiveSupport::Concern
5
-
6
5
  include RSpec::Rails::SetupAndTeardownAdapter
7
6
  include RSpec::Rails::TestUnitAssertionAdapter
8
- include RSpec::Matchers
9
7
  end
10
8
  end
11
9
  end