rspec-rails 2.14.0.rc1 → 2.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog.md +12 -0
  3. data/README.md +2 -2
  4. data/features/Autotest.md +24 -0
  5. data/features/Generators.md +24 -0
  6. data/features/GettingStarted.md +84 -0
  7. data/features/README.md +56 -0
  8. data/features/RailsVersions.md +4 -0
  9. data/features/Transactions.md +84 -0
  10. data/features/Upgrade.md +121 -0
  11. data/features/controller_specs/Cookies.md +57 -0
  12. data/features/controller_specs/README.md +45 -0
  13. data/features/controller_specs/anonymous_controller.feature +378 -0
  14. data/features/controller_specs/bypass_rescue.feature +75 -0
  15. data/features/controller_specs/controller_spec.feature +58 -0
  16. data/features/controller_specs/engine_routes.feature +51 -0
  17. data/features/controller_specs/isolation_from_views.feature +87 -0
  18. data/features/controller_specs/render_views.feature +114 -0
  19. data/features/feature_specs/feature_spec.feature +34 -0
  20. data/features/helper_specs/helper_spec.feature +122 -0
  21. data/features/mailer_specs/url_helpers.feature +38 -0
  22. data/features/matchers/README.md +18 -0
  23. data/features/matchers/new_record_matcher.feature +41 -0
  24. data/features/matchers/redirect_to_matcher.feature +40 -0
  25. data/features/matchers/relation_match_array.feature +22 -0
  26. data/features/matchers/render_template_matcher.feature +49 -0
  27. data/features/mocks/mock_model.feature +147 -0
  28. data/features/mocks/stub_model.feature +58 -0
  29. data/features/model_specs/README.md +21 -0
  30. data/features/model_specs/errors_on.feature +51 -0
  31. data/features/model_specs/records.feature +27 -0
  32. data/features/model_specs/transactional_examples.feature +109 -0
  33. data/features/request_specs/request_spec.feature +49 -0
  34. data/features/routing_specs/README.md +16 -0
  35. data/features/routing_specs/be_routable_matcher.feature +80 -0
  36. data/features/routing_specs/engine_routes.feature +38 -0
  37. data/features/routing_specs/named_routes.feature +18 -0
  38. data/features/routing_specs/route_to_matcher.feature +90 -0
  39. data/features/step_definitions/additional_cli_steps.rb +4 -0
  40. data/features/step_definitions/model_steps.rb +3 -0
  41. data/features/support/env.rb +53 -0
  42. data/features/support/rails_versions.rb +4 -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/rspec/rails/example/controller_example_group.rb +2 -4
  47. data/lib/rspec/rails/extensions/active_record/base.rb +8 -2
  48. data/lib/rspec/rails/mocks.rb +15 -10
  49. data/lib/rspec/rails/tasks/rspec.rake +7 -7
  50. data/lib/rspec/rails/version.rb +1 -1
  51. data/spec/autotest/rails_rspec2_spec.rb +36 -0
  52. data/spec/generators/rspec/controller/controller_generator_spec.rb +97 -0
  53. data/spec/generators/rspec/helper/helper_generator_spec.rb +30 -0
  54. data/spec/generators/rspec/install/install_generator_spec.rb +30 -0
  55. data/spec/generators/rspec/integration/integration_generator_spec.rb +44 -0
  56. data/spec/generators/rspec/mailer/mailer_generator_spec.rb +48 -0
  57. data/spec/generators/rspec/model/model_generator_spec.rb +52 -0
  58. data/spec/generators/rspec/observer/observer_generator_spec.rb +21 -0
  59. data/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +138 -0
  60. data/spec/generators/rspec/view/view_generator_spec.rb +41 -0
  61. data/spec/rspec/rails/assertion_adapter_spec.rb +28 -0
  62. data/spec/rspec/rails/assertion_delegator_spec.rb +43 -0
  63. data/spec/rspec/rails/configuration_spec.rb +26 -0
  64. data/spec/rspec/rails/deprecations_spec.rb +18 -0
  65. data/spec/rspec/rails/example/controller_example_group_spec.rb +100 -0
  66. data/spec/rspec/rails/example/feature_example_group_spec.rb +56 -0
  67. data/spec/rspec/rails/example/helper_example_group_spec.rb +64 -0
  68. data/spec/rspec/rails/example/mailer_example_group_spec.rb +21 -0
  69. data/spec/rspec/rails/example/model_example_group_spec.rb +15 -0
  70. data/spec/rspec/rails/example/request_example_group_spec.rb +17 -0
  71. data/spec/rspec/rails/example/routing_example_group_spec.rb +32 -0
  72. data/spec/rspec/rails/example/view_example_group_spec.rb +220 -0
  73. data/spec/rspec/rails/extensions/active_model/errors_on_spec.rb +23 -0
  74. data/spec/rspec/rails/extensions/active_record/base_spec.rb +42 -0
  75. data/spec/rspec/rails/fixture_support_spec.rb +17 -0
  76. data/spec/rspec/rails/matchers/be_a_new_spec.rb +142 -0
  77. data/spec/rspec/rails/matchers/be_new_record_spec.rb +33 -0
  78. data/spec/rspec/rails/matchers/be_routable_spec.rb +41 -0
  79. data/spec/rspec/rails/matchers/be_valid_spec.rb +44 -0
  80. data/spec/rspec/rails/matchers/errors_on_spec.rb +38 -0
  81. data/spec/rspec/rails/matchers/has_spec.rb +29 -0
  82. data/spec/rspec/rails/matchers/have_rendered_spec.rb +93 -0
  83. data/spec/rspec/rails/matchers/redirect_to_spec.rb +80 -0
  84. data/spec/rspec/rails/matchers/relation_match_array_spec.rb +31 -0
  85. data/spec/rspec/rails/matchers/route_to_spec.rb +151 -0
  86. data/spec/rspec/rails/minitest_lifecycle_adapter_spec.rb +22 -0
  87. data/spec/rspec/rails/mocks/mock_model_spec.rb +379 -0
  88. data/spec/rspec/rails/mocks/stub_model_spec.rb +154 -0
  89. data/spec/rspec/rails/setup_and_teardown_adapter_spec.rb +32 -0
  90. data/spec/rspec/rails/view_rendering_spec.rb +111 -0
  91. data/spec/spec_helper.rb +31 -0
  92. data/spec/support/ar_classes.rb +42 -0
  93. data/spec/support/helpers.rb +20 -0
  94. data/spec/support/matchers.rb +9 -0
  95. data/spec/support/null_object.rb +6 -0
  96. metadata +192 -18
@@ -0,0 +1,220 @@
1
+ require "spec_helper"
2
+
3
+ module RSpec::Rails
4
+ describe ViewExampleGroup do
5
+ it { should be_included_in_files_in('./spec/views/') }
6
+ it { should be_included_in_files_in('.\\spec\\views\\') }
7
+
8
+ it "adds :type => :view to the metadata" do
9
+ group = RSpec::Core::ExampleGroup.describe do
10
+ include ViewExampleGroup
11
+ end
12
+ group.metadata[:type].should eq(:view)
13
+ end
14
+
15
+ describe 'automatic inclusion of helpers' do
16
+ module ::ThingsHelper; end
17
+ module ::Namespaced; module ThingsHelper; end; end
18
+
19
+ it 'includes the helper with the same name' do
20
+ group = RSpec::Core::ExampleGroup.describe 'things/show.html.erb'
21
+ group.should_receive(:helper).with(ThingsHelper)
22
+ group.class_eval do
23
+ include ViewExampleGroup
24
+ end
25
+ end
26
+
27
+ it 'includes the namespaced helper with the same name' do
28
+ group = RSpec::Core::ExampleGroup.describe 'namespaced/things/show.html.erb'
29
+ group.should_receive(:helper).with(Namespaced::ThingsHelper)
30
+ group.class_eval do
31
+ include ViewExampleGroup
32
+ end
33
+ end
34
+
35
+ it 'operates normally when no helper with the same name exists' do
36
+ raise 'unexpected constant found' if Object.const_defined?('ClocksHelper')
37
+ lambda {
38
+ RSpec::Core::ExampleGroup.describe 'clocks/show.html.erb' do
39
+ include ViewExampleGroup
40
+ end
41
+ }.should_not raise_error
42
+ end
43
+
44
+ context 'application helper exists' do
45
+ before do
46
+ if !Object.const_defined? 'ApplicationHelper'
47
+ module ::ApplicationHelper; end
48
+ @application_helper_defined = true
49
+ end
50
+ end
51
+
52
+ after do
53
+ if @application_helper_defined
54
+ Object.__send__ :remove_const, 'ApplicationHelper'
55
+ end
56
+ end
57
+
58
+ it 'includes the application helper' do
59
+ group = RSpec::Core::Example.describe 'bars/new.html.erb'
60
+ group.should_receive(:helper).with(ApplicationHelper)
61
+ group.class_eval do
62
+ include ViewExampleGroup
63
+ end
64
+ end
65
+ end
66
+
67
+ context 'no application helper exists' do
68
+ before do
69
+ if Object.const_defined? 'ApplicationHelper'
70
+ @application_helper = ApplicationHelper
71
+ Object.__send__ :remove_const, 'ApplicationHelper'
72
+ end
73
+ end
74
+
75
+ after do
76
+ if @application_helper
77
+ ApplicationHelper = @application_helper
78
+ end
79
+ end
80
+
81
+ it 'operates normally' do
82
+ lambda {
83
+ RSpec::Core::ExampleGroup.describe 'foos/edit.html.erb' do
84
+ include ViewExampleGroup
85
+ end
86
+ }.should_not raise_error
87
+ end
88
+ end
89
+ end
90
+
91
+ describe "#render" do
92
+ let(:view_spec) do
93
+ Class.new do
94
+ module Local
95
+ def received
96
+ @received ||= []
97
+ end
98
+ def render(options={}, local_assigns={}, &block)
99
+ received << [options, local_assigns, block]
100
+ end
101
+ def _assigns
102
+ {}
103
+ end
104
+ end
105
+ include Local
106
+ include ViewExampleGroup::ExampleMethods
107
+ end.new
108
+ end
109
+
110
+ context "given no input" do
111
+ it "sends render(:template => (described file)) to the view" do
112
+ view_spec.stub(:_default_file_to_render) { "widgets/new" }
113
+ view_spec.render
114
+ view_spec.received.first.should == [{:template => "widgets/new"},{}, nil]
115
+ end
116
+
117
+ it "converts the filename components into render options" do
118
+ view_spec.stub(:_default_file_to_render) { "widgets/new.en.html.erb" }
119
+ view_spec.render
120
+
121
+ if ::Rails::VERSION::STRING >= '3.2'
122
+ view_spec.received.first.should == [{:template => "widgets/new", :locales=>['en'], :formats=>['html'], :handlers=>['erb']}, {}, nil]
123
+ else
124
+ view_spec.received.first.should == [{:template => "widgets/new.en.html.erb"}, {}, nil]
125
+ end
126
+ end
127
+ end
128
+
129
+ context "given a string" do
130
+ it "sends string as the first arg to render" do
131
+ view_spec.render('arbitrary/path')
132
+ view_spec.received.first.should == ["arbitrary/path", {}, nil]
133
+ end
134
+ end
135
+
136
+ context "given a hash" do
137
+ it "sends the hash as the first arg to render" do
138
+ view_spec.render(:foo => 'bar')
139
+ view_spec.received.first.should == [{:foo => "bar"}, {}, nil]
140
+ end
141
+ end
142
+ end
143
+
144
+ describe '#params' do
145
+ let(:view_spec) do
146
+ Class.new do
147
+ include ViewExampleGroup::ExampleMethods
148
+ def controller
149
+ @controller ||= Object.new
150
+ end
151
+ end.new
152
+ end
153
+
154
+ it 'delegates to the controller' do
155
+ view_spec.controller.should_receive(:params).and_return({})
156
+ view_spec.params[:foo] = 1
157
+ end
158
+ end
159
+
160
+ describe "#_controller_path" do
161
+ let(:view_spec) do
162
+ Class.new do
163
+ include ViewExampleGroup::ExampleMethods
164
+ end.new
165
+ end
166
+ context "with a common _default_file_to_render" do
167
+ it "it returns the directory" do
168
+ view_spec.stub(:_default_file_to_render).
169
+ and_return("things/new.html.erb")
170
+ view_spec.__send__(:_controller_path).
171
+ should == "things"
172
+ end
173
+ end
174
+
175
+ context "with a nested _default_file_to_render" do
176
+ it "it returns the directory path" do
177
+ view_spec.stub(:_default_file_to_render).
178
+ and_return("admin/things/new.html.erb")
179
+ view_spec.__send__(:_controller_path).
180
+ should == "admin/things"
181
+ end
182
+ end
183
+ end
184
+
185
+ describe "#view" do
186
+ let(:view_spec) do
187
+ Class.new do
188
+ include ViewExampleGroup::ExampleMethods
189
+ end.new
190
+ end
191
+
192
+ it "delegates to _view" do
193
+ view = double("view")
194
+ view_spec.stub(:_view) { view }
195
+ view_spec.view.should == view
196
+ end
197
+ end
198
+
199
+ describe "#template" do
200
+ let(:view_spec) do
201
+ Class.new do
202
+ include ViewExampleGroup::ExampleMethods
203
+ def _view; end
204
+ end.new
205
+ end
206
+
207
+ before { RSpec.stub(:deprecate) }
208
+
209
+ it "is deprecated" do
210
+ RSpec.should_receive(:deprecate)
211
+ view_spec.template
212
+ end
213
+
214
+ it "delegates to #view" do
215
+ view_spec.should_receive(:view)
216
+ view_spec.template
217
+ end
218
+ end
219
+ end
220
+ end
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+
3
+ describe "errors_on" do
4
+ let(:klass) do
5
+ Class.new do
6
+ include ActiveModel::Validations
7
+ end
8
+ end
9
+
10
+ it "calls valid?" do
11
+ model = klass.new
12
+ model.should_receive(:valid?)
13
+ model.errors_on(:foo)
14
+ end
15
+
16
+ it "returns the errors on that attribute" do
17
+ model = klass.new
18
+ model.stub(:errors) do
19
+ { :foo => ['a', 'b'] }
20
+ end
21
+ model.errors_on(:foo).should eq(['a','b'])
22
+ end
23
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::ActiveModel::Validations do
4
+ describe "#errors_on" do
5
+ context "ActiveModel class that takes no arguments to valid?" do
6
+ let(:klass) {
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+
10
+ def self.name
11
+ "ActiveModelValidationsFake"
12
+ end
13
+
14
+ def valid?
15
+ super
16
+ end
17
+
18
+ attr_accessor :name
19
+ validates_presence_of :name
20
+ end
21
+ }
22
+
23
+ context "with nil name" do
24
+ it "has one error" do
25
+ object = klass.new
26
+ object.name = ""
27
+
28
+ expect(object).to have(1).error_on(:name)
29
+ end
30
+ end
31
+
32
+ context "with non-blank name" do
33
+ it "has no error" do
34
+ object = klass.new
35
+ object.name = "Ywen"
36
+
37
+ expect(object).to have(:no).error_on(:name)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ module RSpec::Rails
4
+ describe FixtureSupport do
5
+ context "with use_transactional_fixtures set to false" do
6
+ it "still supports fixture_path" do
7
+ RSpec.configuration.stub(:use_transactional_fixtures) { false }
8
+ group = RSpec::Core::ExampleGroup.describe do
9
+ include FixtureSupport
10
+ end
11
+
12
+ group.should respond_to(:fixture_path)
13
+ group.should respond_to(:fixture_path=)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,142 @@
1
+ require "spec_helper"
2
+
3
+ describe "be_a_new matcher" do
4
+ include RSpec::Rails::Matchers
5
+
6
+ context "new record" do
7
+ let(:record) do
8
+ Class.new do
9
+ def new_record?; true; end
10
+ end.new
11
+ end
12
+ context "right class" do
13
+ it "passes" do
14
+ record.should be_a_new(record.class)
15
+ end
16
+ end
17
+ context "wrong class" do
18
+ it "fails" do
19
+ record.should_not be_a_new(String)
20
+ end
21
+ end
22
+ end
23
+
24
+ context "existing record" do
25
+ let(:record) do
26
+ Class.new do
27
+ def new_record?; false; end
28
+ end.new
29
+ end
30
+ context "right class" do
31
+ it "fails" do
32
+ record.should_not be_a_new(record.class)
33
+ end
34
+ end
35
+ context "wrong class" do
36
+ it "fails" do
37
+ record.should_not be_a_new(String)
38
+ end
39
+ end
40
+ end
41
+
42
+ describe "#with" do
43
+ context "right class and new record" do
44
+ let(:record) do
45
+ Class.new do
46
+ def initialize(attributes)
47
+ @attributes = attributes
48
+ end
49
+
50
+ def attributes
51
+ @attributes.stringify_keys
52
+ end
53
+
54
+ def new_record?; true; end
55
+ end.new(:foo => 'foo', :bar => 'bar')
56
+ end
57
+
58
+ context "all attributes same" do
59
+ it "passes" do
60
+ record.should be_a_new(record.class).with(:foo => 'foo', :bar => 'bar')
61
+ end
62
+ end
63
+
64
+ context "one attribute same" do
65
+ it "passes" do
66
+ record.should be_a_new(record.class).with(:foo => 'foo')
67
+ end
68
+ end
69
+
70
+ context "no attributes same" do
71
+ it "fails" do
72
+ expect {
73
+ record.should be_a_new(record.class).with(:zoo => 'zoo', :car => 'car')
74
+ }.to raise_error {|e|
75
+ e.message.should match(/attributes {.*} were not set on #{record.inspect}/)
76
+ e.message.should match(/"zoo"=>"zoo"/)
77
+ e.message.should match(/"car"=>"car"/)
78
+ }
79
+ end
80
+ end
81
+
82
+ context "one attribute value not the same" do
83
+ it "fails" do
84
+ expect {
85
+ record.should be_a_new(record.class).with(:foo => 'bar')
86
+ }.to raise_error(
87
+ %Q(attribute {"foo"=>"bar"} was not set on #{record.inspect})
88
+ )
89
+ end
90
+ end
91
+ end
92
+
93
+ context "wrong class and existing record" do
94
+ let(:record) do
95
+ Class.new do
96
+ def initialize(attributes)
97
+ @attributes = attributes
98
+ end
99
+
100
+ def attributes
101
+ @attributes.stringify_keys
102
+ end
103
+
104
+ def new_record?; false; end
105
+ end.new(:foo => 'foo', :bar => 'bar')
106
+ end
107
+
108
+ context "all attributes same" do
109
+ it "fails" do
110
+ expect {
111
+ record.should be_a_new(String).with(:foo => 'foo', :bar => 'bar')
112
+ }.to raise_error(
113
+ "expected #{record.inspect} to be a new String"
114
+ )
115
+ end
116
+ end
117
+
118
+ context "no attributes same" do
119
+ it "fails" do
120
+ expect {
121
+ record.should be_a_new(String).with(:zoo => 'zoo', :car => 'car')
122
+ }.to raise_error {|e|
123
+ e.message.should match(/expected #{record.inspect} to be a new String and attributes {.*} were not set on #{record.inspect}/)
124
+ e.message.should match(/"zoo"=>"zoo"/)
125
+ e.message.should match(/"car"=>"car"/)
126
+ }
127
+ end
128
+ end
129
+
130
+ context "one attribute value not the same" do
131
+ it "fails" do
132
+ expect {
133
+ record.should be_a_new(String).with(:foo => 'bar')
134
+ }.to raise_error(
135
+ "expected #{record.inspect} to be a new String and " +
136
+ %Q(attribute {"foo"=>"bar"} was not set on #{record.inspect})
137
+ )
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,33 @@
1
+ require "spec_helper"
2
+
3
+ describe "be_new_record" do
4
+ include RSpec::Rails::Matchers
5
+
6
+ context "un-persisted record" do
7
+ let(:record) { double('record', :persisted? => false) }
8
+
9
+ it "passes" do
10
+ record.should be_new_record
11
+ end
12
+
13
+ it "fails with custom failure message" do
14
+ expect {
15
+ expect(record).not_to be_new_record
16
+ }.to raise_exception(/expected .* to be persisted, but was a new record/)
17
+ end
18
+ end
19
+
20
+ context "persisted record" do
21
+ let(:record) { double('record', :persisted? => true) }
22
+
23
+ it "fails" do
24
+ record.should_not be_new_record
25
+ end
26
+
27
+ it "fails with custom failure message" do
28
+ expect {
29
+ expect(record).to be_new_record
30
+ }.to raise_exception(/expected .* to be a new record, but was persisted/)
31
+ end
32
+ end
33
+ end