rspec-rails 2.14.2 → 2.99.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +14 -6
  2. data/Changelog.md +8 -10
  3. data/features/Autotest.md +24 -0
  4. data/features/Generators.md +24 -0
  5. data/features/GettingStarted.md +84 -0
  6. data/features/README.md +56 -0
  7. data/features/RailsVersions.md +4 -0
  8. data/features/Transactions.md +84 -0
  9. data/features/Upgrade.md +121 -0
  10. data/features/controller_specs/Cookies.md +57 -0
  11. data/features/controller_specs/README.md +45 -0
  12. data/features/controller_specs/anonymous_controller.feature +378 -0
  13. data/features/controller_specs/bypass_rescue.feature +75 -0
  14. data/features/controller_specs/controller_spec.feature +58 -0
  15. data/features/controller_specs/engine_routes.feature +51 -0
  16. data/features/controller_specs/isolation_from_views.feature +87 -0
  17. data/features/controller_specs/render_views.feature +114 -0
  18. data/features/feature_specs/feature_spec.feature +34 -0
  19. data/features/helper_specs/helper_spec.feature +122 -0
  20. data/features/mailer_specs/url_helpers.feature +38 -0
  21. data/features/matchers/README.md +18 -0
  22. data/features/matchers/new_record_matcher.feature +41 -0
  23. data/features/matchers/redirect_to_matcher.feature +40 -0
  24. data/features/matchers/relation_match_array.feature +27 -0
  25. data/features/matchers/render_template_matcher.feature +49 -0
  26. data/features/mocks/mock_model.feature +147 -0
  27. data/features/mocks/stub_model.feature +58 -0
  28. data/features/model_specs/README.md +21 -0
  29. data/features/model_specs/errors_on.feature +51 -0
  30. data/features/model_specs/records.feature +27 -0
  31. data/features/model_specs/transactional_examples.feature +109 -0
  32. data/features/request_specs/request_spec.feature +49 -0
  33. data/features/routing_specs/README.md +16 -0
  34. data/features/routing_specs/be_routable_matcher.feature +80 -0
  35. data/features/routing_specs/engine_routes.feature +38 -0
  36. data/features/routing_specs/named_routes.feature +18 -0
  37. data/features/routing_specs/route_to_matcher.feature +90 -0
  38. data/features/step_definitions/additional_cli_steps.rb +4 -0
  39. data/features/step_definitions/model_steps.rb +3 -0
  40. data/features/support/env.rb +53 -0
  41. data/features/support/rails_versions.rb +4 -0
  42. data/features/support/rubinius.rb +6 -0
  43. data/features/view_specs/inferred_controller_path.feature +45 -0
  44. data/features/view_specs/stub_template.feature +51 -0
  45. data/features/view_specs/view_spec.feature +206 -0
  46. data/lib/autotest/rails_rspec2.rb +81 -76
  47. data/lib/generators/rspec/install/templates/spec/spec_helper.rb.tt +1 -6
  48. data/lib/rspec/rails/adapters.rb +5 -12
  49. data/lib/rspec/rails/example/controller_example_group.rb +1 -2
  50. data/lib/rspec/rails/example/helper_example_group.rb +3 -3
  51. data/lib/rspec/rails/example/view_example_group.rb +4 -7
  52. data/lib/rspec/rails/matchers/have_extension.rb +0 -1
  53. data/lib/rspec/rails/matchers/routing_matchers.rb +0 -4
  54. data/lib/rspec/rails/mocks.rb +0 -26
  55. data/lib/rspec/rails/tasks/rspec.rake +5 -12
  56. data/lib/rspec/rails/version.rb +1 -1
  57. data/spec/autotest/rails_rspec2_spec.rb +36 -0
  58. data/spec/generators/rspec/controller/controller_generator_spec.rb +97 -0
  59. data/spec/generators/rspec/helper/helper_generator_spec.rb +30 -0
  60. data/spec/generators/rspec/install/install_generator_spec.rb +30 -0
  61. data/spec/generators/rspec/integration/integration_generator_spec.rb +44 -0
  62. data/spec/generators/rspec/mailer/mailer_generator_spec.rb +48 -0
  63. data/spec/generators/rspec/model/model_generator_spec.rb +52 -0
  64. data/spec/generators/rspec/observer/observer_generator_spec.rb +21 -0
  65. data/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +138 -0
  66. data/spec/generators/rspec/view/view_generator_spec.rb +41 -0
  67. data/spec/rspec/rails/assertion_adapter_spec.rb +28 -0
  68. data/spec/rspec/rails/assertion_delegator_spec.rb +43 -0
  69. data/spec/rspec/rails/configuration_spec.rb +26 -0
  70. data/spec/rspec/rails/deprecations_spec.rb +18 -0
  71. data/spec/rspec/rails/example/controller_example_group_spec.rb +100 -0
  72. data/spec/rspec/rails/example/feature_example_group_spec.rb +56 -0
  73. data/spec/rspec/rails/example/helper_example_group_spec.rb +66 -0
  74. data/spec/rspec/rails/example/mailer_example_group_spec.rb +21 -0
  75. data/spec/rspec/rails/example/model_example_group_spec.rb +15 -0
  76. data/spec/rspec/rails/example/request_example_group_spec.rb +17 -0
  77. data/spec/rspec/rails/example/routing_example_group_spec.rb +32 -0
  78. data/spec/rspec/rails/example/view_example_group_spec.rb +220 -0
  79. data/spec/rspec/rails/extensions/active_model/errors_on_spec.rb +23 -0
  80. data/spec/rspec/rails/extensions/active_record/base_spec.rb +42 -0
  81. data/spec/rspec/rails/fixture_support_spec.rb +17 -0
  82. data/spec/rspec/rails/matchers/be_a_new_spec.rb +142 -0
  83. data/spec/rspec/rails/matchers/be_new_record_spec.rb +33 -0
  84. data/spec/rspec/rails/matchers/be_routable_spec.rb +41 -0
  85. data/spec/rspec/rails/matchers/be_valid_spec.rb +44 -0
  86. data/spec/rspec/rails/matchers/errors_on_spec.rb +38 -0
  87. data/spec/rspec/rails/matchers/has_spec.rb +29 -0
  88. data/spec/rspec/rails/matchers/have_rendered_spec.rb +93 -0
  89. data/spec/rspec/rails/matchers/redirect_to_spec.rb +80 -0
  90. data/spec/rspec/rails/matchers/relation_match_array_spec.rb +31 -0
  91. data/spec/rspec/rails/matchers/route_to_spec.rb +151 -0
  92. data/spec/rspec/rails/minitest_lifecycle_adapter_spec.rb +22 -0
  93. data/spec/rspec/rails/mocks/mock_model_spec.rb +378 -0
  94. data/spec/rspec/rails/mocks/stub_model_spec.rb +154 -0
  95. data/spec/rspec/rails/setup_and_teardown_adapter_spec.rb +32 -0
  96. data/spec/rspec/rails/view_rendering_spec.rb +111 -0
  97. data/spec/spec_helper.rb +31 -0
  98. data/spec/support/ar_classes.rb +42 -0
  99. data/spec/support/helpers.rb +20 -0
  100. data/spec/support/matchers.rb +9 -0
  101. data/spec/support/null_object.rb +6 -0
  102. metadata +223 -47
@@ -0,0 +1,38 @@
1
+ Feature: URL helpers in mailer examples
2
+
3
+ Scenario: using URL helpers with default options
4
+ Given a file named "config/initializers/mailer_defaults.rb" with:
5
+ """ruby
6
+ Rails.configuration.action_mailer.default_url_options = { :host => 'example.com' }
7
+ """
8
+ And a file named "spec/mailers/notifications_spec.rb" with:
9
+ """ruby
10
+ require 'spec_helper'
11
+
12
+ describe Notifications do
13
+ it 'should have access to URL helpers' do
14
+ expect { gadgets_url }.not_to raise_error
15
+ end
16
+ end
17
+ """
18
+ When I run `rspec spec`
19
+ Then the examples should all pass
20
+
21
+ Scenario: using URL helpers without default options
22
+ Given a file named "config/initializers/mailer_defaults.rb" with:
23
+ """ruby
24
+ # no default options
25
+ """
26
+ And a file named "spec/mailers/notifications_spec.rb" with:
27
+ """ruby
28
+ require 'spec_helper'
29
+
30
+ describe Notifications do
31
+ it 'should have access to URL helpers' do
32
+ expect { gadgets_url :host => 'example.com' }.not_to raise_error
33
+ expect { gadgets_url }.to raise_error
34
+ end
35
+ end
36
+ """
37
+ When I run `rspec spec`
38
+ Then the examples should all pass
@@ -0,0 +1,18 @@
1
+ rspec-rails offers a number of custom matchers, most of which are
2
+ rspec-compatible wrappers for Rails' assertions.
3
+
4
+ ### redirects
5
+
6
+ # delegates to assert_redirected_to
7
+ expect(response).to redirect_to(path)
8
+
9
+ ### templates
10
+
11
+ # delegates to assert_template
12
+ expect(response).to render_template(template_name)
13
+
14
+ ### assigned objects
15
+
16
+ # passes if assigns(:widget) is an instance of Widget
17
+ # and it is not persisted
18
+ expect(assigns(:widget)).to be_a_new(Widget)
@@ -0,0 +1,41 @@
1
+ Feature: be_a_new matcher
2
+
3
+ The `be_a_new` matcher accepts a class and passes if the subject is an
4
+ instance of that class that returns false to persisted?
5
+
6
+ You can also chain `with` on `be_a_new` with a hash of attributes to specify
7
+ the subject has equal attributes.
8
+
9
+ Scenario: example spec with four be_a_new possibilities
10
+ Given a file named "spec/models/widget_spec.rb" with:
11
+ """ruby
12
+ require "spec_helper"
13
+
14
+ describe Widget do
15
+ context "when initialized" do
16
+ subject(:widget) { Widget.new }
17
+
18
+ it "is a new widget" do
19
+ expect(widget).to be_a_new(Widget)
20
+ end
21
+
22
+ it "is not a new string" do
23
+ expect(widget).not_to be_a_new(String)
24
+ end
25
+ end
26
+
27
+ context "when saved" do
28
+ subject(:widget) { Widget.create }
29
+
30
+ it "is not a new widget" do
31
+ expect(widget).not_to be_a_new(Widget)
32
+ end
33
+
34
+ it "is not a new string" do
35
+ expect(widget).not_to be_a_new(String)
36
+ end
37
+ end
38
+ end
39
+ """
40
+ When I run `rspec spec/models/widget_spec.rb`
41
+ Then the examples should all pass
@@ -0,0 +1,40 @@
1
+ Feature: redirect_to matcher
2
+
3
+ The `redirect_to` matcher is used to specify that a request redirects to a
4
+ given template or action. It delegates to
5
+ [`assert_redirected_to`](http://api.rubyonrails.org/classes/ActionDispatch/Assertions/ResponseAssertions.html#method-i-assert_redirected_to).
6
+
7
+ It is available in controller specs (spec/controllers) and request
8
+ specs (spec/requests).
9
+
10
+ Scenario: redirect_to with four possible options
11
+ Given a file named "spec/controllers/widgets_controller_spec.rb" with:
12
+ """ruby
13
+ require "spec_helper"
14
+
15
+ describe WidgetsController do
16
+
17
+ describe "#create" do
18
+ subject { post :create, :widget => { :name => "Foo" } }
19
+
20
+ it "redirects to widget_url(@widget)" do
21
+ expect(subject).to redirect_to(widget_url(assigns(:widget)))
22
+ end
23
+
24
+ it "redirects_to :action => :show" do
25
+ expect(subject).to redirect_to :action => :show,
26
+ :id => assigns(:widget).id
27
+ end
28
+
29
+ it "redirects_to(@widget)" do
30
+ expect(subject).to redirect_to(assigns(:widget))
31
+ end
32
+
33
+ it "redirects_to /widgets/:id" do
34
+ expect(subject).to redirect_to("/widgets/#{assigns(:widget).id}")
35
+ end
36
+ end
37
+ end
38
+ """
39
+ When I run `rspec spec/controllers/widgets_controller_spec.rb`
40
+ Then the examples should all pass
@@ -0,0 +1,27 @@
1
+ Feature: ActiveRecord::Relation match array
2
+
3
+ The `match_array` matcher can be used with an `ActiveRecord::Relation`
4
+ (scope). The assertion will pass if the scope would return all of the
5
+ elements specified in the array on the right hand side.
6
+
7
+ Scenario: example spec with relation match_array matcher
8
+ Given a file named "spec/models/widget_spec.rb" with:
9
+ """ruby
10
+ require "spec_helper"
11
+
12
+ describe Widget do
13
+ let!(:widgets) { Array.new(3) { Widget.create } }
14
+
15
+ if ::Rails::VERSION::STRING >= '4'
16
+ subject { Widget.all }
17
+ else
18
+ subject { Widget.scoped }
19
+ end
20
+
21
+ it "returns all widgets in any order" do
22
+ expect(subject).to match_array(widgets)
23
+ end
24
+ end
25
+ """
26
+ When I run `rspec spec/models/widget_spec.rb`
27
+ Then the examples should all pass
@@ -0,0 +1,49 @@
1
+ Feature: render_template matcher
2
+
3
+ The `render_template` matcher is used to specify that a request renders a
4
+ given template. It delegates to
5
+ [`assert_template`](http://api.rubyonrails.org/classes/ActionController/TemplateAssertions.html#method-i-assert_template)
6
+
7
+ It is available in controller specs (spec/controllers) and request
8
+ specs (spec/requests).
9
+
10
+ NOTE: use `redirect_to(:action => 'new')` for redirects, not `render_template`.
11
+
12
+ Scenario: render_template with three possible options
13
+ Given a file named "spec/controllers/gadgets_spec.rb" with:
14
+ """ruby
15
+ require "spec_helper"
16
+
17
+ describe GadgetsController do
18
+ describe "GET #index" do
19
+ subject { get :index }
20
+
21
+ it "renders the index template" do
22
+ expect(subject).to render_template(:index)
23
+ expect(subject).to render_template("index")
24
+ expect(subject).to render_template("gadgets/index")
25
+ end
26
+ end
27
+ end
28
+ """
29
+ When I run `rspec spec/controllers/gadgets_spec.rb`
30
+ Then the examples should all pass
31
+
32
+ Scenario: render_template in a view spec
33
+ Given a file named "spec/views/gadgets/index.html.erb_spec.rb" with:
34
+ """ruby
35
+ require "spec_helper"
36
+
37
+ describe "gadgets/index" do
38
+ it "renders the index template" do
39
+ assign(:gadgets, [stub_model(Gadget)])
40
+ render
41
+
42
+ expect(view).to render_template(:index)
43
+ expect(view).to render_template("index")
44
+ expect(view).to render_template("gadgets/index")
45
+ end
46
+ end
47
+ """
48
+ When I run `rspec spec/views`
49
+ Then the examples should all pass
@@ -0,0 +1,147 @@
1
+ Feature: mock_model
2
+
3
+ The `mock_model` method generates a test double that acts like an instance of
4
+ `ActiveModel`. This is different from the `stub_model` method which generates
5
+ an instance of a real model class.
6
+
7
+ The benefit of `mock_model` over `stub_model` is that it is a true double, so
8
+ the examples are not dependent on the behavior (or mis-behavior), or even the
9
+ existence of any other code. If you're working on a controller spec and you
10
+ need a model that doesn't exist, you can pass `mock_model` a string and the
11
+ generated object will act as though its an instance of the class named by
12
+ that string.
13
+
14
+ Scenario: passing a string that represents a non-existent constant
15
+ Given a file named "spec/models/car_spec.rb" with:
16
+ """ruby
17
+ require "spec_helper"
18
+
19
+ describe "mock_model('Car') with no Car constant in existence" do
20
+ it "generates a constant" do
21
+ expect(Object.const_defined?(:Car)).to be_falsey
22
+ mock_model("Car")
23
+ expect(Object.const_defined?(:Car)).to be_truthy
24
+ end
25
+
26
+ describe "generates an object that ..." do
27
+ it "returns the correct name" do
28
+ car = mock_model("Car")
29
+ expect(car.class.name).to eq("Car")
30
+ end
31
+
32
+ it "says it is a Car" do
33
+ car = mock_model("Car")
34
+ expect(car).to be_a(Car)
35
+ end
36
+ end
37
+ end
38
+ """
39
+ When I run `rspec spec/models/car_spec.rb`
40
+ Then the examples should all pass
41
+
42
+ Scenario: passing a string that represents an existing constant
43
+ Given a file named "spec/models/widget_spec.rb" with:
44
+ """ruby
45
+ require "spec_helper"
46
+
47
+ describe Widget do
48
+ it "uses the existing constant" do
49
+ widget = mock_model("Widget")
50
+ expect(widget).to be_a(Widget)
51
+ end
52
+ end
53
+ """
54
+ When I run `rspec spec/models/widget_spec.rb`
55
+ Then the examples should all pass
56
+
57
+ Scenario: passing a class that does not extend ActiveModel::Naming
58
+ Given a file named "spec/models/string_spec.rb" with:
59
+ """ruby
60
+ require "spec_helper"
61
+
62
+ describe String do
63
+ it "raises" do
64
+ expect { mock_model(String) }.to raise_exception
65
+ end
66
+ end
67
+ """
68
+ When I run `rspec spec/models/string_spec.rb`
69
+ Then the examples should all pass
70
+
71
+ Scenario: passing an Active Record constant
72
+ Given a file named "spec/models/widget_spec.rb" with:
73
+ """ruby
74
+ require "spec_helper"
75
+
76
+ describe Widget do
77
+ let(:widget) { mock_model(Widget) }
78
+
79
+ it "is valid by default" do
80
+ expect(widget).to be_valid
81
+ end
82
+
83
+ it "is not a new record by default" do
84
+ expect(widget).not_to be_new_record
85
+ end
86
+
87
+ it "can be converted to a new record" do
88
+ expect(widget.as_new_record).to be_new_record
89
+ end
90
+
91
+ it "sets :id to nil upon destroy" do
92
+ widget.destroy
93
+ expect(widget.id).to be_nil
94
+ end
95
+ end
96
+ """
97
+ When I run `rspec spec/models/widget_spec.rb`
98
+ Then the examples should all pass
99
+
100
+ Scenario: passing an Active Record constant with method stubs
101
+ Given a file named "spec/models/widget_spec.rb" with:
102
+ """ruby
103
+ require "spec_helper"
104
+
105
+ describe "mock_model(Widget) with stubs" do
106
+ let(:widget) do
107
+ mock_model Widget, :foo => "bar",
108
+ :save => true,
109
+ :update_attributes => false
110
+ end
111
+
112
+ it "supports stubs for methods that don't exist in ActiveModel or ActiveRecord" do
113
+ expect(widget.foo).to eq("bar")
114
+ end
115
+
116
+ it "supports stubs for methods that do exist" do
117
+ expect(widget.save).to eq(true)
118
+ expect(widget.update_attributes).to be_falsey
119
+ end
120
+
121
+ describe "#errors" do
122
+ context "with update_attributes => false" do
123
+ it "is not empty" do
124
+ expect(widget.errors).not_to be_empty
125
+ end
126
+ end
127
+ end
128
+ end
129
+ """
130
+ When I run `rspec spec/models/widget_spec.rb`
131
+ Then the examples should all pass
132
+
133
+ Scenario: mock_model outside rails
134
+ Given a file named "mock_model_outside_rails_spec.rb" with:
135
+ """ruby
136
+ require 'rspec/rails/mocks'
137
+
138
+ describe "Foo" do
139
+ it "is mockable" do
140
+ foo = mock_model("Foo")
141
+ expect(foo.id).to eq(1001)
142
+ expect(foo.to_param).to eq("1001")
143
+ end
144
+ end
145
+ """
146
+ When I run `rspec mock_model_outside_rails_spec.rb`
147
+ Then the examples should all pass
@@ -0,0 +1,58 @@
1
+ Feature: stub_model
2
+
3
+ The stub_model method generates an instance of a Active Model model.
4
+
5
+ While you can use stub_model in any example (model, view, controller,
6
+ helper), it is especially useful in view examples, which are inherently
7
+ more state-based than interaction-based.
8
+
9
+ Scenario: passing an Active Record constant with a hash of stubs
10
+ Given a file named "spec/models/widget_spec.rb" with:
11
+ """ruby
12
+ require "spec_helper"
13
+
14
+ describe "stub_model(Widget) with a hash of stubs" do
15
+ let(:widget) do
16
+ stub_model Widget, :id => 5, :random_attribute => true
17
+ end
18
+
19
+ it "stubs :id" do
20
+ expect(widget.id).to eql(5)
21
+ end
22
+
23
+ it "stubs :random_attribute" do
24
+ expect(widget.random_attribute).to be_truthy
25
+ end
26
+
27
+ it "returns false for new_record? if :id is set" do
28
+ expect(widget).not_to be_new_record
29
+ end
30
+
31
+ it "can be converted to a new record" do
32
+ widget.as_new_record
33
+ expect(widget).to be_new_record
34
+ end
35
+ end
36
+ """
37
+ When I run `rspec spec/models/widget_spec.rb`
38
+ Then the examples should all pass
39
+
40
+ Scenario: passing an Active Record constant with a block of stubs
41
+ Given a file named "spec/models/widget_spec.rb" with:
42
+ """ruby
43
+ require "spec_helper"
44
+
45
+ describe "stub_model(Widget) with a block of stubs" do
46
+ let(:widget) do
47
+ stub_model Widget do |widget|
48
+ widget.id = 5
49
+ end
50
+ end
51
+
52
+ it "stubs :id" do
53
+ expect(widget.id).to eql(5)
54
+ end
55
+ end
56
+ """
57
+ When I run `rspec spec/models/widget_spec.rb`
58
+ Then the examples should all pass
@@ -0,0 +1,21 @@
1
+ Model specs live in `spec/models` or any example group with
2
+ `:type => :model`.
3
+
4
+ A model spec is a thin wrapper for an ActiveSupport::TestCase, and includes all
5
+ of the behavior and assertions that it provides, in addition to RSpec's own
6
+ behavior and expectations.
7
+
8
+ ## Examples
9
+
10
+ require "spec_helper"
11
+
12
+ describe Post do
13
+ context "with 2 or more comments" do
14
+ it "orders them in reverse chronologically" do
15
+ post = Post.create!
16
+ comment1 = post.comments.create!(:body => "first comment")
17
+ comment2 = post.comments.create!(:body => "second comment")
18
+ expect(post.reload.comments).to eq([comment2, comment1])
19
+ end
20
+ end
21
+ end