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,41 @@
1
+ require "spec_helper"
2
+
3
+ describe "be_routable" do
4
+ include RSpec::Rails::Matchers::RoutingMatchers
5
+ attr_reader :routes
6
+
7
+ before { @routes = double("routes") }
8
+
9
+ context "with should" do
10
+ it "passes if routes recognize the path" do
11
+ routes.stub(:recognize_path) { {} }
12
+ expect do
13
+ {:get => "/a/path"}.should be_routable
14
+ end.to_not raise_error
15
+ end
16
+
17
+ it "fails if routes do not recognize the path" do
18
+ routes.stub(:recognize_path) { raise ActionController::RoutingError.new('ignore') }
19
+ expect do
20
+ {:get => "/a/path"}.should be_routable
21
+ end.to raise_error(/expected \{:get=>"\/a\/path"\} to be routable/)
22
+ end
23
+ end
24
+
25
+ context "with should_not" do
26
+
27
+ it "passes if routes do not recognize the path" do
28
+ routes.stub(:recognize_path) { raise ActionController::RoutingError.new('ignore') }
29
+ expect do
30
+ {:get => "/a/path"}.should_not be_routable
31
+ end.to_not raise_error
32
+ end
33
+
34
+ it "fails if routes recognize the path" do
35
+ routes.stub(:recognize_path) { {:controller => "foo"} }
36
+ expect do
37
+ {:get => "/a/path"}.should_not be_routable
38
+ end.to raise_error(/expected \{:get=>"\/a\/path"\} not to be routable, but it routes to \{:controller=>"foo"\}/)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,44 @@
1
+ require "spec_helper"
2
+ require 'active_support/all'
3
+ require 'rspec/rails/matchers/be_valid'
4
+
5
+ describe "be_valid matcher" do
6
+ include RSpec::Rails::Matchers
7
+
8
+ class Post
9
+ include ActiveModel::Validations
10
+ attr_accessor :title
11
+ validates_presence_of :title
12
+ end
13
+
14
+ let(:post) { Post.new }
15
+
16
+ it "includes the error messages in the failure message" do
17
+ expect {
18
+ expect(post).to be_valid
19
+ }.to raise_exception(/Title can't be blank/)
20
+ end
21
+
22
+ it "includes a failure message for the negative case" do
23
+ post.stub(:valid?) { true }
24
+ expect {
25
+ expect(post).not_to be_valid
26
+ }.to raise_exception(/expected .* not to be valid/)
27
+ end
28
+
29
+ it "uses a custom failure message if provided" do
30
+ expect {
31
+ expect(post).to be_valid, "Post was not valid!"
32
+ }.to raise_exception(/Post was not valid!/)
33
+ end
34
+
35
+ it "includes the validation context if provided" do
36
+ post.should_receive(:valid?).with(:create) { true }
37
+ expect(post).to be_valid(:create)
38
+ end
39
+
40
+ it "does not include the validation context if not provided" do
41
+ post.should_receive(:valid?).with(no_args) { true }
42
+ expect(post).to be_valid
43
+ end
44
+ end
@@ -0,0 +1,38 @@
1
+ require "spec_helper"
2
+
3
+ describe "error_on" do
4
+ it "provides a description including the name of what the error is on" do
5
+ have(1).error_on(:whatever).description.should == "have 1 error on :whatever"
6
+ end
7
+
8
+ it "provides a failure message including the number actually given" do
9
+ lambda {
10
+ [].should have(1).error_on(:whatever)
11
+ }.should raise_error("expected 1 error on :whatever, got 0")
12
+ end
13
+ end
14
+
15
+ describe "errors_on" do
16
+ it "provides a description including the name of what the error is on" do
17
+ have(2).errors_on(:whatever).description.should == "have 2 errors on :whatever"
18
+ end
19
+
20
+ it "provides a failure message including the number actually given" do
21
+ lambda {
22
+ [1].should have(3).errors_on(:whatever)
23
+ }.should raise_error("expected 3 errors on :whatever, got 1")
24
+ end
25
+ end
26
+
27
+ describe "have something other than error_on or errors_on" do
28
+ it "has a standard rspec failure message" do
29
+ lambda {
30
+ [1,2,3].should have(2).elements
31
+ }.should raise_error("expected 2 elements, got 3")
32
+ end
33
+
34
+ it "has a standard rspec description" do
35
+ have(2).elements.description.should == "have 2 elements"
36
+ end
37
+ end
38
+
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ class CollectionOwner < ActiveRecord::Base
4
+ connection.execute <<-SQL
5
+ CREATE TABLE collection_owners (
6
+ id integer PRIMARY KEY AUTOINCREMENT
7
+ )
8
+ SQL
9
+ has_many :associated_items do
10
+ def has_some_quality?; true end
11
+ end
12
+ end
13
+
14
+ class AssociatedItem < ActiveRecord::Base
15
+ connection.execute <<-SQL
16
+ CREATE TABLE associated_items (
17
+ id integer PRIMARY KEY AUTOINCREMENT,
18
+ collection_owner_id integer
19
+ )
20
+ SQL
21
+ belongs_to :collection_owner
22
+ end
23
+
24
+ describe "should have_xxx" do
25
+ it "works with ActiveRecord::Associations::CollectionProxy" do
26
+ owner = CollectionOwner.new
27
+ owner.associated_items.should have_some_quality
28
+ end
29
+ end
@@ -0,0 +1,93 @@
1
+ require "spec_helper"
2
+
3
+ %w[have_rendered render_template].each do |template_expectation|
4
+ describe template_expectation do
5
+ include RSpec::Rails::Matchers::RenderTemplate
6
+ let(:response) { ActionController::TestResponse.new }
7
+
8
+ context "given a hash" do
9
+ it "delegates to assert_template" do
10
+ self.should_receive(:assert_template).with({:this => "hash"}, "this message")
11
+ expect("response").to send(template_expectation, {:this => "hash"}, "this message")
12
+ end
13
+ end
14
+
15
+ context "given a string" do
16
+ it "delegates to assert_template" do
17
+ self.should_receive(:assert_template).with("this string", "this message")
18
+ expect("response").to send(template_expectation, "this string", "this message")
19
+ end
20
+ end
21
+
22
+ context "given a symbol" do
23
+ it "converts to_s and delegates to assert_template" do
24
+ self.should_receive(:assert_template).with("template_name", "this message")
25
+ expect("response").to send(template_expectation, :template_name, "this message")
26
+ end
27
+ end
28
+
29
+ context "with should" do
30
+ context "when assert_template passes" do
31
+ it "passes" do
32
+ def assert_template(*); end
33
+ expect do
34
+ expect(response).to send(template_expectation, "template_name")
35
+ end.to_not raise_exception
36
+ end
37
+ end
38
+
39
+ context "when assert_template fails" do
40
+ it "uses failure message from assert_template" do
41
+ def assert_template(*)
42
+ raise ActiveSupport::TestCase::Assertion.new("this message")
43
+ end
44
+ expect do
45
+ expect(response).to send(template_expectation, "template_name")
46
+ end.to raise_error("this message")
47
+ end
48
+ end
49
+
50
+ context "when fails due to some other exception" do
51
+ it "raises that exception" do
52
+ def assert_template(*)
53
+ raise "oops"
54
+ end
55
+ expect do
56
+ expect(response).to send(template_expectation, "template_name")
57
+ end.to raise_exception("oops")
58
+ end
59
+ end
60
+ end
61
+
62
+ context "with should_not" do
63
+ context "when assert_template fails" do
64
+ it "passes" do
65
+ def assert_template(*)
66
+ raise ActiveSupport::TestCase::Assertion.new("this message")
67
+ end
68
+ expect do
69
+ expect(response).to_not send(template_expectation, "template_name")
70
+ end.to_not raise_exception
71
+ end
72
+ end
73
+
74
+ context "when assert_template passes" do
75
+ it "fails with custom failure message" do
76
+ def assert_template(*); end
77
+ expect do
78
+ expect(response).to_not send(template_expectation, "template_name")
79
+ end.to raise_error(/expected not to render \"template_name\", but did/)
80
+ end
81
+ end
82
+
83
+ context "when fails due to some other exception" do
84
+ it "raises that exception" do
85
+ def assert_template(*); raise "oops"; end
86
+ expect do
87
+ expect(response).to_not send(template_expectation, "template_name")
88
+ end.to raise_exception("oops")
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,80 @@
1
+ require "spec_helper"
2
+ require "active_support/test_case"
3
+
4
+ describe "redirect_to" do
5
+ include RSpec::Rails::Matchers::RedirectTo
6
+
7
+ let(:response) { ActionController::TestResponse.new }
8
+
9
+ context "with should" do
10
+ context "when assert_redirected_to passes" do
11
+ def assert_redirected_to(*); end
12
+
13
+ it "passes" do
14
+ expect do
15
+ response.should redirect_to("destination")
16
+ end.to_not raise_exception
17
+ end
18
+ end
19
+
20
+ context "when assert_redirected_to fails" do
21
+ def assert_redirected_to(*)
22
+ raise ActiveSupport::TestCase::Assertion.new("this message")
23
+ end
24
+
25
+ it "uses failure message from assert_redirected_to" do
26
+ expect do
27
+ response.should redirect_to("destination")
28
+ end.to raise_exception("this message")
29
+ end
30
+ end
31
+
32
+ context "when fails due to some other exception" do
33
+ def assert_redirected_to(*)
34
+ raise "oops"
35
+ end
36
+
37
+ it "raises that exception" do
38
+ expect do
39
+ response.should redirect_to("destination")
40
+ end.to raise_exception("oops")
41
+ end
42
+ end
43
+ end
44
+
45
+ context "with should_not" do
46
+ context "when assert_redirected_to fails" do
47
+ def assert_redirected_to(*)
48
+ raise ActiveSupport::TestCase::Assertion.new("this message")
49
+ end
50
+
51
+ it "passes" do
52
+ expect do
53
+ response.should_not redirect_to("destination")
54
+ end.to_not raise_exception
55
+ end
56
+ end
57
+
58
+ context "when assert_redirected_to passes" do
59
+ def assert_redirected_to(*); end
60
+
61
+ it "fails with custom failure message" do
62
+ expect do
63
+ response.should_not redirect_to("destination")
64
+ end.to raise_exception(/expected not to redirect to \"destination\", but did/)
65
+ end
66
+ end
67
+
68
+ context "when fails due to some other exception" do
69
+ def assert_redirected_to(*)
70
+ raise "oops"
71
+ end
72
+
73
+ it "raises that exception" do
74
+ expect do
75
+ response.should_not redirect_to("destination")
76
+ end.to raise_exception("oops")
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ describe "ActiveSupport::Relation =~ matcher" do
4
+ before { MockableModel.delete_all }
5
+
6
+ let!(:models) { Array.new(3) { MockableModel.create } }
7
+
8
+ if ::Rails::VERSION::STRING >= '4'
9
+ it "verifies that the scope returns the records on the right hand side, regardless of order" do
10
+ MockableModel.all.should =~ models.reverse
11
+ end
12
+
13
+ it "fails if the scope encompasses more records than on the right hand side" do
14
+ MockableModel.create
15
+ MockableModel.all.should_not =~ models.reverse
16
+ end
17
+ else
18
+ it "verifies that the scope returns the records on the right hand side, regardless of order" do
19
+ MockableModel.scoped.should =~ models.reverse
20
+ end
21
+
22
+ it "fails if the scope encompasses more records than on the right hand side" do
23
+ MockableModel.create
24
+ MockableModel.scoped.should_not =~ models.reverse
25
+ end
26
+ end
27
+
28
+ it "fails if the scope encompasses fewer records than on the right hand side" do
29
+ MockableModel.limit(models.length - 1).should_not =~ models.reverse
30
+ end
31
+ end
@@ -0,0 +1,151 @@
1
+ require "spec_helper"
2
+
3
+ describe "route_to" do
4
+ include RSpec::Rails::Matchers::RoutingMatchers
5
+ include RSpec::Rails::Matchers::RoutingMatchers::RouteHelpers
6
+
7
+ def assert_recognizes(*)
8
+ # no-op
9
+ end
10
+
11
+ it "provides a description" do
12
+ matcher = route_to("these" => "options")
13
+ matcher.matches?(:get => "path")
14
+ matcher.description.should == "route {:get=>\"path\"} to {\"these\"=>\"options\"}"
15
+ end
16
+
17
+ it "delegates to assert_recognizes" do
18
+ self.should_receive(:assert_recognizes).with({ "these" => "options" }, { :method=> :get, :path=>"path" }, {})
19
+ {:get => "path"}.should route_to("these" => "options")
20
+ end
21
+
22
+ context "with shortcut syntax" do
23
+ it "routes with extra options" do
24
+ self.should_receive(:assert_recognizes).with({ :controller => "controller", :action => "action", :extra => "options"}, { :method=> :get, :path=>"path" }, {})
25
+ get("path").should route_to("controller#action", :extra => "options")
26
+ end
27
+
28
+ it "routes without extra options" do
29
+ self.should_receive(:assert_recognizes).with(
30
+ {:controller => "controller", :action => "action"},
31
+ {:method=> :get, :path=>"path" },
32
+ {}
33
+ )
34
+ get("path").should route_to("controller#action")
35
+ end
36
+
37
+ it "routes with one query parameter" do
38
+ self.should_receive(:assert_recognizes).with(
39
+ {:controller => "controller", :action => "action", :queryitem => "queryvalue"},
40
+ {:method=> :get, :path=>"path" },
41
+ {'queryitem' => 'queryvalue' }
42
+ )
43
+ get("path?queryitem=queryvalue").should route_to("controller#action", :queryitem => 'queryvalue')
44
+ end
45
+
46
+ it "routes with multiple query parameters" do
47
+ self.should_receive(:assert_recognizes).with(
48
+ {:controller => "controller", :action => "action", :queryitem => "queryvalue", :qi2 => 'qv2'},
49
+ {:method=> :get, :path=>"path"},
50
+ {'queryitem' => 'queryvalue', 'qi2' => 'qv2'}
51
+ )
52
+ get("path?queryitem=queryvalue&qi2=qv2").should route_to("controller#action", :queryitem => 'queryvalue', :qi2 => 'qv2')
53
+ end
54
+
55
+ end
56
+
57
+ context "with should" do
58
+ context "when assert_recognizes passes" do
59
+ it "passes" do
60
+ expect do
61
+ {:get => "path"}.should route_to("these" => "options")
62
+ end.to_not raise_exception
63
+ end
64
+ end
65
+
66
+ context "when assert_recognizes fails with an assertion failure" do
67
+ it "fails with message from assert_recognizes" do
68
+ def assert_recognizes(*)
69
+ raise ActiveSupport::TestCase::Assertion.new("this message")
70
+ end
71
+ expect do
72
+ {:get => "path"}.should route_to("these" => "options")
73
+ end.to raise_error(RSpec::Expectations::ExpectationNotMetError, "this message")
74
+ end
75
+ end
76
+
77
+ context "when assert_recognizes fails with a routing error" do
78
+ it "fails with message from assert_recognizes" do
79
+ def assert_recognizes(*)
80
+ raise ActionController::RoutingError.new("this message")
81
+ end
82
+ expect do
83
+ {:get => "path"}.should route_to("these" => "options")
84
+ end.to raise_error(RSpec::Expectations::ExpectationNotMetError, "this message")
85
+ end
86
+ end
87
+
88
+ context "when an exception is raised" do
89
+ it "raises that exception" do
90
+ def assert_recognizes(*)
91
+ raise "oops"
92
+ end
93
+ expect do
94
+ {:get => "path"}.should route_to("these" => "options")
95
+ end.to raise_exception("oops")
96
+ end
97
+ end
98
+ end
99
+
100
+ context "with should_not" do
101
+ context "when assert_recognizes passes" do
102
+ it "fails with custom message" do
103
+ expect do
104
+ {:get => "path"}.should_not route_to("these" => "options")
105
+ end.to raise_error(/expected .* not to route to .*/)
106
+ end
107
+ end
108
+
109
+ context "when assert_recognizes fails with an assertion failure" do
110
+ it "passes" do
111
+ def assert_recognizes(*)
112
+ raise ActiveSupport::TestCase::Assertion.new("this message")
113
+ end
114
+ expect do
115
+ {:get => "path"}.should_not route_to("these" => "options")
116
+ end.to_not raise_error
117
+ end
118
+ end
119
+
120
+ context "when assert_recognizes fails with a routing error" do
121
+ it "passes" do
122
+ def assert_recognizes(*)
123
+ raise ActionController::RoutingError.new("this message")
124
+ end
125
+ expect do
126
+ {:get => "path"}.should_not route_to("these" => "options")
127
+ end.to_not raise_error
128
+ end
129
+ end
130
+
131
+ context "when an exception is raised" do
132
+ it "raises that exception" do
133
+ def assert_recognizes(*)
134
+ raise "oops"
135
+ end
136
+ expect do
137
+ {:get => "path"}.should_not route_to("these" => "options")
138
+ end.to raise_exception("oops")
139
+ end
140
+ end
141
+ end
142
+
143
+ it "uses failure message from assert_recognizes" do
144
+ def assert_recognizes(*)
145
+ raise ActiveSupport::TestCase::Assertion, "this message"
146
+ end
147
+ expect do
148
+ {"this" => "path"}.should route_to("these" => "options")
149
+ end.to raise_error("this message")
150
+ end
151
+ end