rspec-rails 2.0.0.beta.13 → 2.0.0.beta.14.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/README.markdown +75 -54
  2. data/Rakefile +5 -2
  3. data/Upgrade.markdown +13 -13
  4. data/VERSION +1 -1
  5. data/features/controller_specs/{do_not_render_views.feature → isolation_from_views.feature} +12 -11
  6. data/features/controller_specs/render_views.feature +8 -8
  7. data/features/{routing_specs → matchers}/be_routable_matcher.feature +18 -2
  8. data/lib/generators/rspec/install/templates/spec/spec_helper.rb +2 -2
  9. data/lib/generators/rspec/scaffold/scaffold_generator.rb +9 -0
  10. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +1 -1
  11. data/lib/generators/rspec/scaffold/templates/index_spec.rb +2 -2
  12. data/lib/generators/rspec/scaffold/templates/show_spec.rb +2 -2
  13. data/lib/rspec/rails.rb +3 -0
  14. data/lib/rspec/rails/example/controller_example_group.rb +5 -8
  15. data/lib/rspec/rails/example/helper_example_group.rb +3 -6
  16. data/lib/rspec/rails/example/mailer_example_group.rb +2 -7
  17. data/lib/rspec/rails/example/request_example_group.rb +2 -6
  18. data/lib/rspec/rails/example/routing_example_group.rb +3 -4
  19. data/lib/rspec/rails/example/view_example_group.rb +3 -6
  20. data/lib/rspec/rails/matchers.rb +1 -11
  21. data/lib/rspec/rails/matchers/{routing_spec_matchers.rb → routing_matchers.rb} +2 -2
  22. data/lib/rspec/rails/mocks.rb +11 -1
  23. data/lib/rspec/rails/module_inclusion.rb +11 -0
  24. data/lib/rspec/rails/monkey/action_mailer/test_case.rb +48 -48
  25. data/lib/rspec/rails/view_rendering.rb +30 -6
  26. data/rspec-rails.gemspec +16 -12
  27. data/spec/rspec/rails/example/controller_example_group_spec.rb +8 -6
  28. data/spec/rspec/rails/example/helper_example_group_spec.rb +3 -7
  29. data/spec/rspec/rails/example/mailer_example_group_spec.rb +2 -7
  30. data/spec/rspec/rails/example/request_example_group_spec.rb +2 -7
  31. data/spec/rspec/rails/example/routing_example_group_spec.rb +2 -7
  32. data/spec/rspec/rails/example/view_example_group_spec.rb +2 -7
  33. data/spec/rspec/rails/matchers/route_to_spec.rb +2 -2
  34. data/spec/rspec/rails/mocks/mock_model_spec.rb +31 -0
  35. data/spec/spec_helper.rb +18 -0
  36. data/templates/generate_stuff.rb +1 -1
  37. metadata +28 -14
@@ -1,13 +1,13 @@
1
1
  # rspec-rails-2
2
2
 
3
- This README aligns with the code in git HEAD. If you're looking for the README for
4
- the latest release, go to [http://github.com/rspec/rspec-rails](http://github.com/rspec/rspec-rails),
5
- and select the appropriate tag from the Switch Tags select list.
3
+ rspec-2 for rails-3 with lightweight extensions to each
6
4
 
7
- ## RSpec-2 for Rails-3
5
+ NOTICE: rspec-rails-2.0.0.beta.13 only works with rails-3.0.0.beta4 or higher.
8
6
 
9
- rspec-rails-2 brings rspec-2 to rails-3 with lightweight extensions to both
10
- libraries.
7
+ NOTICE: This README aligns with the code in git HEAD. If you're looking for the
8
+ README for the latest release, go to
9
+ [http://github.com/rspec/rspec-rails](http://github.com/rspec/rspec-rails), and
10
+ select the appropriate tag from the Switch Tags select list.
11
11
 
12
12
  ## Install
13
13
 
@@ -25,7 +25,7 @@ This installs the following gems:
25
25
 
26
26
  Add this line to the Gemfile:
27
27
 
28
- gem "rspec-rails", ">= 2.0.0.beta.8"
28
+ gem "rspec-rails", ">= 2.0.0.beta.13"
29
29
 
30
30
  This will expose generators, including rspec:install. Now you can run:
31
31
 
@@ -60,35 +60,23 @@ or rails. Many of the APIs from rspec-rails-1 have been carried forward,
60
60
  however, so upgrading an app from rspec-1/rails-2, while not pain-free, should
61
61
  not send you to the doctor with a migraine.
62
62
 
63
- ## Synopsis
64
-
65
- * each example runs in its own transaction
66
- * configurable in RSpec.configure
67
- * see generated spec/spec\_helper.rb
68
- * model specs in spec/models
69
- * controller specs in spec/controllers
70
- * view specs in spec/views
71
- * mailer specs in spec/mailers
72
- * observer specs in spec/models
73
- * request specs in spec/requests
74
- * these wrap rails integration tests
75
- * rails assertions
76
- * assertion-wrapping matchers
77
- * redirect\_to
78
- * render\_template
79
- * helper specs
80
- * webrat matchers
81
- * generators
82
- * run "script/rails g" to see the list of available generators
83
-
84
63
  ## Known issues
85
64
 
86
65
  See http://github.com/rspec/rspec-rails/issues
87
66
 
88
67
  # Request Specs
89
68
 
90
- Request specs live in spec/requests, and mix in behavior
91
- from Rails' integration tests.
69
+ Request specs live in spec/requests.
70
+
71
+ describe "widgets resource" do
72
+ describe "GET index" do
73
+ get "/widgets/index"
74
+ response.should have_selector("h1", :content => "Widgets")
75
+ end
76
+ end
77
+
78
+ Request specs mix in behavior from Rails' integration tests. See the
79
+ docs for ActionDispatch::Integration::Runner for more information.
92
80
 
93
81
  # Controller Specs
94
82
 
@@ -100,11 +88,12 @@ available from Rails.
100
88
  You can use RSpec expectations/matchers or Test::Unit assertions.
101
89
 
102
90
  ## `render_views`
103
- By default, controller specs do not render views (as of beta.9).
104
- This supports specifying controllers without concern for whether
105
- the views they render work correctly or even exist. If you prefer
106
- to render the views (a la Rails' functional tests), you can use the
107
- `render_views` declaration in each example group:
91
+ By default, controller specs do not render views. This supports specifying
92
+ controllers without concern for whether the views they render work correctly
93
+ (NOTE: the template must exist, unlike rspec-rails-1. See Upgrade.markdown for
94
+ more information about this). If you prefer to render the views (a la Rails'
95
+ functional tests), you can use the `render_views` declaration in each example
96
+ group:
108
97
 
109
98
  describe SomeController do
110
99
  render_views
@@ -122,16 +111,6 @@ assigns to the view in the course of an action:
122
111
  get :index
123
112
  assigns(:widgets).should eq(expected_value)
124
113
 
125
- ## `render_template`
126
- Delegates to Rails' assert_template:
127
-
128
- response.should render_template("new")
129
-
130
- ## `redirect_to`
131
- Delegates to assert_redirect
132
-
133
- response.should redirect_to(widgets_path)
134
-
135
114
  # View specs
136
115
 
137
116
  View specs live in spec/views, and mix in ActionView::TestCase::Behavior.
@@ -197,15 +176,6 @@ Routing specs live in spec/routing.
197
176
  end
198
177
  end
199
178
 
200
- ## `route_to`
201
-
202
- Delegates to Rails' assert_routing.
203
-
204
- ## `be_routable`
205
-
206
- Passes if the path is recognized by Rails' routing. This is primarily intended
207
- to be used with `should_not` to specify routes that should not be routable.
208
-
209
179
  # Helper specs
210
180
 
211
181
  Helper specs live in spec/helpers, and mix in ActionView::TestCase::Behavior.
@@ -220,3 +190,54 @@ Helper specs live in spec/helpers, and mix in ActionView::TestCase::Behavior.
220
190
  end
221
191
  end
222
192
  end
193
+
194
+ # Matchers
195
+
196
+ rspec-rails exposes domain-specific matchers to each of the example group types. Most
197
+ of them simply delegate to Rails' assertions.
198
+
199
+ ## `be_a_new`
200
+ * Available in all specs.
201
+ * Primarily intended for controller specs
202
+
203
+ <pre>
204
+ object.should be_a_new(Widget)
205
+ </pre>
206
+
207
+ Passes if the object is a `Widget` and returns true for `new_record?`
208
+
209
+ ## `render_template`
210
+ * Delegates to Rails' assert_template.
211
+ * Available in request, controller, and view specs.
212
+
213
+ In request and controller specs, apply to the response object:
214
+
215
+ response.should render_template("new")
216
+
217
+ In view specs, apply to the view object:
218
+
219
+ view.should render_template(:partial => "_form", :locals => { :widget => widget } )
220
+
221
+ ## `redirect_to`
222
+ * Delegates to assert_redirect
223
+ * Available in request and controller specs.
224
+
225
+ <pre>
226
+ response.should redirect_to(widgets_path)
227
+ </pre>
228
+
229
+ ## `route_to`
230
+
231
+ * Delegates to Rails' assert_routing.
232
+ * Available in routing and controller specs.
233
+
234
+ <pre>
235
+ { :get => "/widgets" }.should route_to(:controller => "widgets", :action => "index")
236
+ </pre>
237
+
238
+ ## `be_routable`
239
+
240
+ Passes if the path is recognized by Rails' routing. This is primarily intended
241
+ to be used with `should_not` to specify routes that should not be routable.
242
+
243
+ { :get => "/widgets/1/edit" }.should_not be_routable
data/Rakefile CHANGED
@@ -38,7 +38,7 @@ begin
38
38
  gem.homepage = "http://github.com/rspec/rspec-rails"
39
39
  gem.authors = ["David Chelimsky", "Chad Humphries"]
40
40
  gem.rubyforge_project = "rspec"
41
- gem.add_dependency "rspec", gem.version
41
+ gem.add_dependency "rspec", ">= 2.0.0.beta.14"
42
42
  gem.add_dependency "webrat", ">= 0.7.0"
43
43
  gem.post_install_message = <<-EOM
44
44
  #{"*"*50}
@@ -53,6 +53,9 @@ begin
53
53
 
54
54
  script/rails generate rspec:install
55
55
 
56
+ Also, be sure to look at Upgrade.markdown to see
57
+ what might have changed since the last release.
58
+
56
59
  #{"*"*50}
57
60
  EOM
58
61
  end
@@ -121,5 +124,5 @@ namespace :clobber do
121
124
  end
122
125
  end
123
126
 
124
- task :default => ["clobber:app", "generate:app", "generate:stuff", :smoke, :spec, :cucumber]
127
+ task :default => [:spec, "clobber:app", "generate:app", "generate:stuff", :cucumber, :smoke]
125
128
 
@@ -2,23 +2,23 @@
2
2
 
3
3
  ## Controller specs
4
4
 
5
- ### render_views
5
+ ### islation from view templates
6
6
 
7
- Controller specs, by default, do _not_ render views. This helps keep the
8
- controller specs focused on the controller. Use `render_views` instead of
9
- rspec-1's `integrate_views` to tell the spec to render the views:
7
+ By default, controller specs do _not_ render view templates. This keeps
8
+ controller specs isolated from the content of views and their requirements.
10
9
 
11
- describe WidgetController do
12
- render_views
13
-
14
- describe "GET index" do
15
- ...
10
+ NOTE that the template must exist, but it will not be rendered. This is
11
+ different from rspec-rails-1.x, in which the template didn't need to exist, but
12
+ ActionController makes a number of new decisions in Rails 3 based on the
13
+ existence of the template. To keep the RSpec code free of monkey patches, and
14
+ to keep the rspec user experience simpler, we decided that this would be a fair
15
+ trade-off.
16
16
 
17
17
  ## View specs
18
18
 
19
- Rails changed the way it renders partials, so to set an expectation that a partial
20
- gets rendered:
19
+ Rails changed the way it renders partials, so to set an expectation that a
20
+ partial gets rendered:
21
21
 
22
- view.should_receive(:_render_partial).
23
- with(hash_including(:partial => "widget/row"))
22
+ render
23
+ view.should render_template(:partial => "widget/_row")
24
24
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.0.beta.13
1
+ 2.0.0.beta.14.1
@@ -1,11 +1,12 @@
1
1
  Feature: do not render views
2
2
 
3
- By default, controller specs do not render views. This
4
- allows you specify which view template an action should
5
- try to render regardless of whether that template exists
6
- or compiles cleanly.
3
+ By default, controller specs do not render views. This allows you specify
4
+ which view template an action should try to render regardless of whether or
5
+ not the template compiles cleanly.
7
6
 
8
- Scenario: expect template that exists and is rendered by controller (passes)
7
+ NOTE: unlike rspec-rails-1.x, the template must exist.
8
+
9
+ Scenario: expect template that rendered by controller (passes)
9
10
  Given a file named "spec/controllers/widgets_controller_spec.rb" with:
10
11
  """
11
12
  require "spec_helper.rb"
@@ -26,7 +27,7 @@ Feature: do not render views
26
27
  When I run "rspec ./spec"
27
28
  Then I should see "2 examples, 0 failures"
28
29
 
29
- Scenario: expect template that exists but is not rendered by controller (fails)
30
+ Scenario: expect template that is not rendered by controller (fails)
30
31
  Given a file named "spec/controllers/widgets_controller_spec.rb" with:
31
32
  """
32
33
  require "spec_helper.rb"
@@ -43,19 +44,19 @@ Feature: do not render views
43
44
  When I run "rspec ./spec"
44
45
  Then I should see "1 example, 1 failure"
45
46
 
46
- Scenario: expect template that does not exist and is not rendered by controller (fails)
47
+ Scenario: set expectation about template content (fails with helpful message)
47
48
  Given a file named "spec/controllers/widgets_controller_spec.rb" with:
48
49
  """
49
50
  require "spec_helper.rb"
50
51
 
51
52
  describe WidgetsController do
52
53
  describe "index" do
53
- it "renders a template that does not exist" do
54
+ it "renders the 'new' template" do
54
55
  get :index
55
- response.should render_template("does_not_exist")
56
+ response.should contain("foo")
56
57
  end
57
58
  end
58
59
  end
59
60
  """
60
- When I run "rspec ./spec"
61
- Then I should see "1 example, 1 failure"
61
+ When I run "rspec spec"
62
+ Then I should see "Template source generated by RSpec"
@@ -1,7 +1,7 @@
1
1
  Feature: render views
2
2
 
3
- You can tell a controller example group to render views
4
- with the render_views declaration.
3
+ You can tell a controller example group to render views with the render_views
4
+ declaration.
5
5
 
6
6
  Scenario: expect template that exists and is rendered by controller (passes)
7
7
  Given a file named "spec/controllers/widgets_controller_spec.rb" with:
@@ -14,20 +14,20 @@ Feature: render views
14
14
  describe "index" do
15
15
  it "renders the index template" do
16
16
  get :index
17
- response.should render_template("index")
17
+ response.should contain("Listing widgets")
18
18
  end
19
19
 
20
20
  it "renders the widgets/index template" do
21
21
  get :index
22
- response.should render_template("widgets/index")
22
+ response.should contain("Listing widgets")
23
23
  end
24
24
  end
25
25
  end
26
26
  """
27
- When I run "rspec ./spec"
27
+ When I run "rspec spec"
28
28
  Then I should see "2 examples, 0 failures"
29
29
 
30
- Scenario: expect template that does not exist and is rendered by controller (passes)
30
+ Scenario: expect template that does not exist and is rendered by controller (fails)
31
31
  Given a file named "spec/controllers/widgets_controller_spec.rb" with:
32
32
  """
33
33
  require "spec_helper.rb"
@@ -44,10 +44,10 @@ Feature: render views
44
44
  describe "index" do
45
45
  it "renders the other template" do
46
46
  get :index
47
- response.should render_template("other")
48
47
  end
49
48
  end
50
49
  end
51
50
  """
52
- When I run "rspec ./spec"
51
+ When I run "rspec spec"
53
52
  Then I should see "1 example, 1 failure"
53
+ And I should see "Missing template"
@@ -1,7 +1,8 @@
1
1
  Feature: be_routable matcher
2
2
 
3
- The be_routable matcher is intended for use with should_not to specify
4
- that a given route should_not be_routable.
3
+ The be_routable matcher is intended for use with should_not to specify that a
4
+ given route should_not be_routable. It is available in routing specs (in
5
+ spec/routing) and controller specs (in spec/controller).
5
6
 
6
7
  Scenario: specify routeable route should be routable (passes)
7
8
  Given a file named "spec/routing/widgets_routing_spec.rb" with:
@@ -62,3 +63,18 @@ Feature: be_routable matcher
62
63
 
63
64
  When I run "rspec spec/routing/widgets_routing_spec.rb"
64
65
  Then I should see "1 example, 1 failure"
66
+
67
+ Scenario: be_routable in a controller spec
68
+ Given a file named "spec/controllers/widgets_controller_spec.rb" with:
69
+ """
70
+ require "spec_helper"
71
+
72
+ describe WidgetsController do
73
+ it "routes to /widgets" do
74
+ { :get => "/widgets" }.should be_routable
75
+ end
76
+ end
77
+ """
78
+
79
+ When I run "rspec spec/controllers/widgets_controller_spec.rb"
80
+ Then I should see "1 example, 0 failures"
@@ -1,14 +1,14 @@
1
1
  # This file is copied to ~/spec when you run 'ruby script/generate rspec'
2
2
  # from the project root directory.
3
3
  ENV["RAILS_ENV"] ||= 'test'
4
- require File.dirname(__FILE__) + "/../config/environment" unless defined?(Rails)
4
+ require File.expand_path("../../config/environment", __FILE__)
5
5
  require 'rspec/rails'
6
6
 
7
7
  # Requires supporting files with custom matchers and macros, etc,
8
8
  # in ./support/ and its subdirectories.
9
9
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
10
 
11
- Rspec.configure do |config|
11
+ RSpec.configure do |config|
12
12
  # == Mock Framework
13
13
  #
14
14
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
@@ -120,6 +120,15 @@ module Rspec
120
120
  response
121
121
  end
122
122
 
123
+ def value_for(attribute)
124
+ case attribute.type
125
+ when :string
126
+ "#{attribute.name.titleize}".inspect
127
+ else
128
+ attribute.default.inspect
129
+ end
130
+ end
131
+
123
132
  end
124
133
  end
125
134
  end
@@ -118,7 +118,7 @@ describe <%= controller_class_name %>Controller do
118
118
  end
119
119
 
120
120
  it "redirects to the <%= table_name %> list" do
121
- <%= stub! orm_class.find(class_name) %> { <%= mock_file_name(:destroy => true) %> }
121
+ <%= stub! orm_class.find(class_name) %> { <%= mock_file_name %> }
122
122
  delete :destroy, :id => "1"
123
123
  response.should redirect_to(<%= table_name %>_url)
124
124
  end
@@ -7,7 +7,7 @@ describe "<%= table_name %>/index.html.<%= options[:template_engine] %>" do
7
7
  <% [1,2].each_with_index do |id, model_index| -%>
8
8
  stub_model(<%= class_name %><%= output_attributes.empty? ? (model_index == 1 ? ')' : '),') : ',' %>
9
9
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
10
- :<%= attribute.name %> => <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
10
+ :<%= attribute.name %> => <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
11
11
  <% end -%>
12
12
  <% if !output_attributes.empty? -%>
13
13
  <%= model_index == 1 ? ')' : '),' %>
@@ -19,7 +19,7 @@ describe "<%= table_name %>/index.html.<%= options[:template_engine] %>" do
19
19
  it "renders a list of <%= table_name %>" do
20
20
  render
21
21
  <% for attribute in output_attributes -%>
22
- rendered.should have_selector("tr>td", :content => <%= attribute.default.inspect %>.to_s, :count => 2)
22
+ rendered.should have_selector("tr>td", :content => <%= value_for(attribute) %>.to_s, :count => 2)
23
23
  <% end -%>
24
24
  end
25
25
  end
@@ -5,7 +5,7 @@ describe "<%= table_name %>/show.html.<%= options[:template_engine] %>" do
5
5
  before(:each) do
6
6
  @<%= file_name %> = assign(:<%= file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? '))' : ',' %>
7
7
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
8
- :<%= attribute.name %> => <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
8
+ :<%= attribute.name %> => <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
9
9
  <% end -%>
10
10
  <% if !output_attributes.empty? -%>
11
11
  ))
@@ -15,7 +15,7 @@ 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
- rendered.should contain(<%= attribute.default.inspect %>)
18
+ rendered.should contain(<%= value_for(attribute) %>.to_s)
19
19
  <% end -%>
20
20
  end
21
21
  end