rspec-rails 2.3.1 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/History.md +21 -1
  2. data/README.md +3 -3
  3. data/Rakefile +2 -2
  4. data/{Upgrade.markdown → Upgrade.md} +0 -0
  5. data/features/.nav +12 -7
  6. data/features/Autotest.md +10 -0
  7. data/features/Generators.md +8 -0
  8. data/features/GettingStarted.md +40 -0
  9. data/features/{README.markdown → README.md} +1 -26
  10. data/features/Upgrade.md +56 -0
  11. data/features/controller_specs/README.md +27 -16
  12. data/features/controller_specs/isolation_from_views.feature +6 -5
  13. data/features/controller_specs/render_views.feature +30 -1
  14. data/features/helper_specs/helper_spec.feature +36 -3
  15. data/features/mocks/mock_model.feature +3 -3
  16. data/features/model_specs/README.md +19 -0
  17. data/features/routing_specs/README.md +16 -0
  18. data/features/{matchers → routing_specs}/be_routable_matcher.feature +18 -18
  19. data/features/routing_specs/{access_to_named_routes.feature → named_routes.feature} +6 -3
  20. data/features/routing_specs/route_to_matcher.feature +38 -0
  21. data/lib/generators/rspec/scaffold/scaffold_generator.rb +6 -6
  22. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +22 -24
  23. data/lib/rspec/rails.rb +0 -1
  24. data/lib/rspec/rails/example.rb +29 -1
  25. data/lib/rspec/rails/example/controller_example_group.rb +1 -5
  26. data/lib/rspec/rails/example/helper_example_group.rb +5 -6
  27. data/lib/rspec/rails/example/mailer_example_group.rb +1 -5
  28. data/lib/rspec/rails/example/model_example_group.rb +0 -3
  29. data/lib/rspec/rails/example/request_example_group.rb +1 -5
  30. data/lib/rspec/rails/example/routing_example_group.rb +1 -5
  31. data/lib/rspec/rails/example/view_example_group.rb +1 -4
  32. data/lib/rspec/rails/matchers/redirect_to.rb +6 -2
  33. data/lib/rspec/rails/matchers/render_template.rb +5 -1
  34. data/lib/rspec/rails/matchers/routing_matchers.rb +1 -1
  35. data/lib/rspec/rails/module_inclusion.rb +17 -0
  36. data/lib/rspec/rails/version.rb +1 -1
  37. data/lib/rspec/rails/view_rendering.rb +23 -7
  38. data/spec/rspec/rails/configuration_spec.rb +26 -0
  39. data/spec/rspec/rails/example/helper_example_group_spec.rb +18 -0
  40. data/spec/rspec/rails/example/request_example_group_spec.rb +2 -0
  41. data/spec/rspec/rails/matchers/be_routable_spec.rb +41 -0
  42. data/spec/rspec/rails/matchers/redirect_to_spec.rb +67 -8
  43. data/spec/rspec/rails/matchers/render_template_spec.rb +67 -10
  44. data/spec/rspec/rails/matchers/route_to_spec.rb +64 -33
  45. data/spec/rspec/rails/view_rendering_spec.rb +105 -0
  46. metadata +38 -20
  47. data/spec/rspec/rails/example/view_rendering_spec.rb +0 -110
@@ -0,0 +1,16 @@
1
+ Routing specs live in the `spec/routing` directory.
2
+
3
+ Simple apps with nothing but standard RESTful routes won't get much value from
4
+ routing specs, but they can provide significant value when used to specify
5
+ customized routes, like vanity links, slugs, etc.
6
+
7
+ { :get => "/articles/2012/11/when-to-use-routing-specs" }.
8
+ should route_to(
9
+ :controller => "articles",
10
+ :month => "2012-11",
11
+ :slug => "when-to-use-routing-specs"
12
+ )
13
+
14
+ They are also valuable for routes that should not be available:
15
+
16
+ { :delete => "/accounts/37" }.should_not be_routable
@@ -1,47 +1,47 @@
1
1
  Feature: be_routable matcher
2
2
 
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).
3
+ The `be_routable` matcher is best used 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/controllers).
6
6
 
7
- Scenario: specify routeable route should be routable (passes)
7
+ Scenario: specify routeable route should not be routable (fails)
8
8
  Given a file named "spec/routing/widgets_routing_spec.rb" with:
9
9
  """
10
10
  require "spec_helper"
11
11
 
12
- describe WidgetsController do
13
- it "routes to /widgets" do
14
- { :get => "/widgets" }.should be_routable
12
+ describe "routes for Widgets" do
13
+ it "does not route to widgets" do
14
+ { :get => "/widgets" }.should_not be_routable
15
15
  end
16
16
  end
17
17
  """
18
18
 
19
19
  When I run "rspec spec/routing/widgets_routing_spec.rb"
20
- Then the output should contain "1 example, 0 failures"
20
+ Then the output should contain "1 example, 1 failure"
21
21
 
22
- Scenario: specify routeable route should not be routable (fails)
22
+ Scenario: specify non-routeable route should not be routable (passes)
23
23
  Given a file named "spec/routing/widgets_routing_spec.rb" with:
24
24
  """
25
25
  require "spec_helper"
26
26
 
27
- describe WidgetsController do
28
- it "does not route to widgets" do
29
- { :get => "/widgets" }.should_not be_routable
27
+ describe "routes for Widgets" do
28
+ it "does not route to widgets/foo/bar" do
29
+ { :get => "/widgets/foo/bar" }.should_not be_routable
30
30
  end
31
31
  end
32
32
  """
33
33
 
34
34
  When I run "rspec spec/routing/widgets_routing_spec.rb"
35
- Then the output should contain "1 example, 1 failure"
35
+ Then the output should contain "1 example, 0 failures"
36
36
 
37
- Scenario: specify non-routeable route should not be routable (passes)
37
+ Scenario: specify routeable route should be routable (passes)
38
38
  Given a file named "spec/routing/widgets_routing_spec.rb" with:
39
39
  """
40
40
  require "spec_helper"
41
41
 
42
- describe WidgetsController do
43
- it "does not route to widgets/foo/bar" do
44
- { :get => "/widgets/foo/bar" }.should_not be_routable
42
+ describe "routes for Widgets" do
43
+ it "routes to /widgets" do
44
+ { :get => "/widgets" }.should be_routable
45
45
  end
46
46
  end
47
47
  """
@@ -54,7 +54,7 @@ Feature: be_routable matcher
54
54
  """
55
55
  require "spec_helper"
56
56
 
57
- describe WidgetsController do
57
+ describe "routes for Widgets" do
58
58
  it "routes to widgets/foo/bar" do
59
59
  { :get => "/widgets/foo/bar" }.should be_routable
60
60
  end
@@ -1,13 +1,16 @@
1
- Feature: access to named routes in routing specs
1
+ Feature: named routes
2
2
 
3
- Scenario: access existing named route
3
+ Routing specs have access to named routes.
4
+
5
+ Scenario: access named route
4
6
  Given a file named "spec/routing/widget_routes_spec.rb" with:
5
7
  """
6
8
  require "spec_helper"
7
9
 
8
10
  describe "routes to the widgets controller" do
9
11
  it "routes a named route" do
10
- {:get => new_widget_path}.should route_to(:controller => "widgets", :action => "new")
12
+ {:get => new_widget_path}.
13
+ should route_to(:controller => "widgets", :action => "new")
11
14
  end
12
15
  end
13
16
  """
@@ -0,0 +1,38 @@
1
+ Feature: route_to matcher
2
+
3
+ The `route_to` matcher specifies that a request (verb + uri) is routable. It
4
+ is most valuable when specifying routes other than standard RESTful routes.
5
+
6
+ { :get => "/" }.should route_to(:controller => "welcome")
7
+
8
+ Scenario: passing route spec
9
+ Given a file named "spec/routing/widgets_routing_spec.rb" with:
10
+ """
11
+ require "spec_helper"
12
+
13
+ describe "routes for Widgets" do
14
+ it "routes /widgets to the widgets controller" do
15
+ { :get => "/widgets" }.
16
+ should route_to(:controller => "widgets", :action => "index")
17
+ end
18
+ end
19
+ """
20
+
21
+ When I run "rspec spec/routing/widgets_routing_spec.rb"
22
+ Then the output should contain "1 example, 0 failures"
23
+
24
+ Scenario: route spec for a route that doesn't exist (fails)
25
+ Given a file named "spec/routing/widgets_routing_spec.rb" with:
26
+ """
27
+ require "spec_helper"
28
+
29
+ describe "routes for Widgets" do
30
+ it "routes /widgets/foo to the /foo action" do
31
+ { :get => "/widgets/foo" }.
32
+ should route_to(:controller => "widgets", :action => "foo")
33
+ end
34
+ end
35
+ """
36
+
37
+ When I run "rspec spec/routing/widgets_routing_spec.rb"
38
+ Then the output should contain "1 example, 1 failure"
@@ -97,21 +97,21 @@ module Rspec
97
97
  # should! orm_class.find(User, "37")
98
98
  # #=> User.should_receive(:get).with(37)
99
99
  #
100
- def should_receive!(chain)
100
+ def should_receive(chain)
101
101
  stub_or_should_chain(:should_receive, chain)
102
102
  end
103
103
 
104
104
  # Receives the ORM chain and convert to stub. For ActiveRecord:
105
105
  #
106
- # stub! orm_class.find(User, "37")
107
- # #=> User.stub!(:find).with(37)
106
+ # stub orm_class.find(User, "37")
107
+ # #=> User.stub(:find).with(37)
108
108
  #
109
109
  # For Datamapper:
110
110
  #
111
- # stub! orm_class.find(User, "37")
112
- # #=> User.stub!(:get).with(37)
111
+ # stub orm_class.find(User, "37")
112
+ # #=> User.stub(:get).with(37)
113
113
  #
114
- def stub!(chain)
114
+ def stub(chain)
115
115
  stub_or_should_chain(:stub, chain)
116
116
  end
117
117
 
@@ -1,17 +1,19 @@
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 the Rails when you ran the scaffold generator.
6
+
3
7
  describe <%= controller_class_name %>Controller do
4
8
 
5
9
  def <%= mock_file_name %>(stubs={})
6
- (@<%= mock_file_name %> ||= mock_model(<%= class_name %>).as_null_object).tap do |<%= file_name %>|
7
- <%= file_name %>.stub(stubs) unless stubs.empty?
8
- end
10
+ @<%= mock_file_name %> ||= mock_model(<%= class_name %>, stubs).as_null_object
9
11
  end
10
12
 
11
13
  <% unless options[:singleton] -%>
12
14
  describe "GET index" do
13
15
  it "assigns all <%= table_name.pluralize %> as @<%= table_name.pluralize %>" do
14
- <%= stub! orm_class.all(class_name) %> { [<%= mock_file_name %>] }
16
+ <%= stub orm_class.all(class_name) %> { [<%= mock_file_name %>] }
15
17
  get :index
16
18
  assigns(:<%= table_name %>).should eq([<%= mock_file_name %>])
17
19
  end
@@ -20,7 +22,7 @@ describe <%= controller_class_name %>Controller do
20
22
  <% end -%>
21
23
  describe "GET show" do
22
24
  it "assigns the requested <%= file_name %> as @<%= file_name %>" do
23
- <%= stub! orm_class.find(class_name, "37".inspect) %> { <%= mock_file_name %> }
25
+ <%= stub orm_class.find(class_name, "37".inspect) %> { <%= mock_file_name %> }
24
26
  get :show, :id => "37"
25
27
  assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
26
28
  end
@@ -28,7 +30,7 @@ describe <%= controller_class_name %>Controller do
28
30
 
29
31
  describe "GET new" do
30
32
  it "assigns a new <%= file_name %> as @<%= file_name %>" do
31
- <%= stub! orm_class.build(class_name) %> { <%= mock_file_name %> }
33
+ <%= stub orm_class.build(class_name) %> { <%= mock_file_name %> }
32
34
  get :new
33
35
  assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
34
36
  end
@@ -36,23 +38,22 @@ describe <%= controller_class_name %>Controller do
36
38
 
37
39
  describe "GET edit" do
38
40
  it "assigns the requested <%= file_name %> as @<%= file_name %>" do
39
- <%= stub! orm_class.find(class_name, "37".inspect) %> { <%= mock_file_name %> }
41
+ <%= stub orm_class.find(class_name, "37".inspect) %> { <%= mock_file_name %> }
40
42
  get :edit, :id => "37"
41
43
  assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
42
44
  end
43
45
  end
44
46
 
45
47
  describe "POST create" do
46
-
47
48
  describe "with valid params" do
48
49
  it "assigns a newly created <%= file_name %> as @<%= file_name %>" do
49
- <%= stub! orm_class.build(class_name, params) %> { <%= mock_file_name(:save => true) %> }
50
+ <%= stub orm_class.build(class_name, params) %> { <%= mock_file_name(:save => true) %> }
50
51
  post :create, :<%= file_name %> => <%= params %>
51
52
  assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
52
53
  end
53
54
 
54
55
  it "redirects to the created <%= file_name %>" do
55
- <%= stub! orm_class.build(class_name) %> { <%= mock_file_name(:save => true) %> }
56
+ <%= stub orm_class.build(class_name) %> { <%= mock_file_name(:save => true) %> }
56
57
  post :create, :<%= file_name %> => {}
57
58
  response.should redirect_to(<%= table_name.singularize %>_url(<%= mock_file_name %>))
58
59
  end
@@ -60,37 +61,35 @@ describe <%= controller_class_name %>Controller do
60
61
 
61
62
  describe "with invalid params" do
62
63
  it "assigns a newly created but unsaved <%= file_name %> as @<%= file_name %>" do
63
- <%= stub! orm_class.build(class_name, params) %> { <%= mock_file_name(:save => false) %> }
64
+ <%= stub orm_class.build(class_name, params) %> { <%= mock_file_name(:save => false) %> }
64
65
  post :create, :<%= file_name %> => <%= params %>
65
66
  assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
66
67
  end
67
68
 
68
69
  it "re-renders the 'new' template" do
69
- <%= stub! orm_class.build(class_name) %> { <%= mock_file_name(:save => false) %> }
70
+ <%= stub orm_class.build(class_name) %> { <%= mock_file_name(:save => false) %> }
70
71
  post :create, :<%= file_name %> => {}
71
72
  response.should render_template("new")
72
73
  end
73
74
  end
74
-
75
75
  end
76
76
 
77
77
  describe "PUT update" do
78
-
79
78
  describe "with valid params" do
80
79
  it "updates the requested <%= file_name %>" do
81
- <%= should_receive! orm_class.find(class_name, "37".inspect) %> { <%= mock_file_name %> }
82
- mock_<%= should_receive! orm_instance.update_attributes(params) %>
80
+ <%= stub orm_class.find(class_name, "37".inspect) %> { <%= mock_file_name %> }
81
+ mock_<%= should_receive orm_instance.update_attributes(params) %>
83
82
  put :update, :id => "37", :<%= file_name %> => <%= params %>
84
83
  end
85
84
 
86
85
  it "assigns the requested <%= file_name %> as @<%= file_name %>" do
87
- <%= stub! orm_class.find(class_name) %> { <%= mock_file_name(:update_attributes => true) %> }
86
+ <%= stub orm_class.find(class_name) %> { <%= mock_file_name(:update_attributes => true) %> }
88
87
  put :update, :id => "1"
89
88
  assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
90
89
  end
91
90
 
92
91
  it "redirects to the <%= file_name %>" do
93
- <%= stub! orm_class.find(class_name) %> { <%= mock_file_name(:update_attributes => true) %> }
92
+ <%= stub orm_class.find(class_name) %> { <%= mock_file_name(:update_attributes => true) %> }
94
93
  put :update, :id => "1"
95
94
  response.should redirect_to(<%= table_name.singularize %>_url(<%= mock_file_name %>))
96
95
  end
@@ -98,29 +97,28 @@ describe <%= controller_class_name %>Controller do
98
97
 
99
98
  describe "with invalid params" do
100
99
  it "assigns the <%= file_name %> as @<%= file_name %>" do
101
- <%= stub! orm_class.find(class_name) %> { <%= mock_file_name(:update_attributes => false) %> }
100
+ <%= stub orm_class.find(class_name) %> { <%= mock_file_name(:update_attributes => false) %> }
102
101
  put :update, :id => "1"
103
102
  assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
104
103
  end
105
104
 
106
105
  it "re-renders the 'edit' template" do
107
- <%= stub! orm_class.find(class_name) %> { <%= mock_file_name(:update_attributes => false) %> }
106
+ <%= stub orm_class.find(class_name) %> { <%= mock_file_name(:update_attributes => false) %> }
108
107
  put :update, :id => "1"
109
108
  response.should render_template("edit")
110
109
  end
111
110
  end
112
-
113
111
  end
114
112
 
115
113
  describe "DELETE destroy" do
116
114
  it "destroys the requested <%= file_name %>" do
117
- <%= should_receive! orm_class.find(class_name, "37".inspect) %> { <%= mock_file_name %> }
118
- mock_<%= should_receive! orm_instance.destroy %>
115
+ <%= stub orm_class.find(class_name, "37".inspect) %> { <%= mock_file_name %> }
116
+ mock_<%= should_receive orm_instance.destroy %>
119
117
  delete :destroy, :id => "37"
120
118
  end
121
119
 
122
120
  it "redirects to the <%= table_name %> list" do
123
- <%= stub! orm_class.find(class_name) %> { <%= mock_file_name %> }
121
+ <%= stub orm_class.find(class_name) %> { <%= mock_file_name %> }
124
122
  delete :destroy, :id => "1"
125
123
  response.should redirect_to(<%= table_name %>_url)
126
124
  end
@@ -15,4 +15,3 @@ require 'rspec/rails/module_inclusion'
15
15
  require 'rspec/rails/browser_simulators'
16
16
  require 'rspec/rails/example'
17
17
 
18
-
@@ -5,4 +5,32 @@ require 'rspec/rails/example/helper_example_group'
5
5
  require 'rspec/rails/example/view_example_group'
6
6
  require 'rspec/rails/example/mailer_example_group'
7
7
  require 'rspec/rails/example/routing_example_group'
8
- require 'rspec/rails/example/model_example_group'
8
+ require 'rspec/rails/example/model_example_group'
9
+
10
+ RSpec::configure do |c|
11
+ def c.escaped_path(*parts)
12
+ Regexp.compile(parts.join('[\\\/]'))
13
+ end
14
+
15
+ c.include RSpec::Rails::ControllerExampleGroup, :type => :controller, :example_group => {
16
+ :file_path => c.escaped_path(%w[spec controllers])
17
+ }
18
+ c.include RSpec::Rails::HelperExampleGroup, :type => :helper, :example_group => {
19
+ :file_path => c.escaped_path(%w[spec helpers])
20
+ }
21
+ c.include RSpec::Rails::MailerExampleGroup, :type => :mailer, :example_group => {
22
+ :file_path => c.escaped_path(%w[spec mailers])
23
+ }
24
+ c.include RSpec::Rails::ModelExampleGroup, :type => :model, :example_group => {
25
+ :file_path => c.escaped_path(%w[spec models])
26
+ }
27
+ c.include RSpec::Rails::RequestExampleGroup, :type => :request, :example_group => {
28
+ :file_path => c.escaped_path(%w[spec (requests|integration)])
29
+ }
30
+ c.include RSpec::Rails::RoutingExampleGroup, :type => :routing, :example_group => {
31
+ :file_path => c.escaped_path(%w[spec routing])
32
+ }
33
+ c.include RSpec::Rails::ViewExampleGroup, :type => :view, :example_group => {
34
+ :file_path => c.escaped_path(%w[spec views])
35
+ }
36
+ end
@@ -72,11 +72,9 @@ module RSpec::Rails
72
72
  #
73
73
  module ControllerExampleGroup
74
74
  extend ActiveSupport::Concern
75
- extend RSpec::Rails::ModuleInclusion
76
-
77
- include RSpec::Rails::RailsExampleGroup
78
75
 
79
76
  include ActionController::TestCase::Behavior
77
+ include RSpec::Rails::RailsExampleGroup
80
78
  include RSpec::Rails::ViewRendering
81
79
  include RSpec::Rails::Matchers::RedirectTo
82
80
  include RSpec::Rails::Matchers::RenderTemplate
@@ -172,7 +170,5 @@ module RSpec::Rails
172
170
  end
173
171
  end
174
172
  end
175
-
176
- RSpec.configure &include_self_when_dir_matches('spec','controllers')
177
173
  end
178
174
  end
@@ -27,11 +27,9 @@ module RSpec::Rails
27
27
  #
28
28
  module HelperExampleGroup
29
29
  extend ActiveSupport::Concern
30
- extend RSpec::Rails::ModuleInclusion
31
-
32
- include RSpec::Rails::RailsExampleGroup
33
30
 
34
31
  include ActionView::TestCase::Behavior
32
+ include RSpec::Rails::RailsExampleGroup
35
33
  include RSpec::Rails::ViewAssigns
36
34
  include RSpec::Rails::BrowserSimulators
37
35
 
@@ -49,7 +47,10 @@ module RSpec::Rails
49
47
  # Returns an instance of ActionView::Base with the helper being specified
50
48
  # mixed in, along with any of the built-in rails helpers.
51
49
  def helper
52
- _view.tap {|v| v.assign(view_assigns)}
50
+ _view.tap do |v|
51
+ v.extend(ApplicationHelper) if defined?(ApplicationHelper)
52
+ v.assign(view_assigns)
53
+ end
53
54
  end
54
55
 
55
56
  private
@@ -66,7 +67,5 @@ module RSpec::Rails
66
67
  controller.controller_path = _controller_path
67
68
  end
68
69
  end
69
-
70
- RSpec.configure &include_self_when_dir_matches('spec','helpers')
71
70
  end
72
71
  end
@@ -2,11 +2,9 @@ if defined?(ActionMailer)
2
2
  module RSpec::Rails
3
3
  module MailerExampleGroup
4
4
  extend ActiveSupport::Concern
5
- extend RSpec::Rails::ModuleInclusion
6
-
7
- include RSpec::Rails::RailsExampleGroup
8
5
 
9
6
  include ActionMailer::TestCase::Behavior
7
+ include RSpec::Rails::RailsExampleGroup
10
8
  include RSpec::Rails::BrowserSimulators
11
9
 
12
10
  webrat do
@@ -29,8 +27,6 @@ if defined?(ActionMailer)
29
27
  describes
30
28
  end
31
29
  end
32
-
33
- RSpec.configure &include_self_when_dir_matches('spec','mailers')
34
30
  end
35
31
  end
36
32
  end
@@ -1,14 +1,11 @@
1
1
  module RSpec::Rails
2
2
  module ModelExampleGroup
3
3
  extend ActiveSupport::Concern
4
- extend RSpec::Rails::ModuleInclusion
5
4
 
6
5
  include RSpec::Rails::RailsExampleGroup
7
6
 
8
7
  included do
9
8
  metadata[:type] = :model
10
9
  end
11
-
12
- RSpec.configure &include_self_when_dir_matches('spec','models')
13
10
  end
14
11
  end