shoulda-matchers 1.4.2 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. data/.travis.yml +11 -3
  2. data/Appraisals +3 -3
  3. data/Gemfile +1 -1
  4. data/Gemfile.lock +69 -70
  5. data/MIT-LICENSE +1 -1
  6. data/NEWS.md +22 -4
  7. data/README.md +3 -1
  8. data/Rakefile +9 -5
  9. data/features/step_definitions/rails_steps.rb +7 -7
  10. data/features/support/env.rb +1 -1
  11. data/gemfiles/3.0.gemfile +1 -1
  12. data/gemfiles/3.0.gemfile.lock +25 -22
  13. data/gemfiles/3.1.gemfile +1 -1
  14. data/gemfiles/3.1.gemfile.lock +27 -24
  15. data/gemfiles/3.2.gemfile +1 -1
  16. data/gemfiles/3.2.gemfile.lock +56 -52
  17. data/lib/shoulda/matchers/action_controller.rb +3 -2
  18. data/lib/shoulda/matchers/action_controller/assign_to_matcher.rb +10 -8
  19. data/lib/shoulda/matchers/action_controller/filter_param_matcher.rb +2 -4
  20. data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +3 -4
  21. data/lib/shoulda/matchers/action_controller/render_template_matcher.rb +3 -4
  22. data/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb +6 -6
  23. data/lib/shoulda/matchers/action_controller/respond_with_content_type_matcher.rb +5 -2
  24. data/lib/shoulda/matchers/action_controller/respond_with_matcher.rb +2 -2
  25. data/lib/shoulda/matchers/action_controller/route_matcher.rb +12 -12
  26. data/lib/shoulda/matchers/action_controller/set_session_matcher.rb +3 -4
  27. data/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb +10 -11
  28. data/lib/shoulda/matchers/action_controller/strong_parameters_matcher.rb +121 -0
  29. data/lib/shoulda/matchers/action_mailer/have_sent_email_matcher.rb +7 -4
  30. data/lib/shoulda/matchers/active_model.rb +1 -1
  31. data/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb +8 -8
  32. data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +5 -4
  33. data/lib/shoulda/matchers/active_model/disallow_value_matcher.rb +3 -3
  34. data/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb +2 -2
  35. data/lib/shoulda/matchers/active_model/exception_message_finder.rb +0 -1
  36. data/lib/shoulda/matchers/active_model/helpers.rb +16 -7
  37. data/lib/shoulda/matchers/active_model/only_integer_matcher.rb +3 -3
  38. data/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb +8 -6
  39. data/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb +19 -10
  40. data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +4 -4
  41. data/lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb +19 -21
  42. data/lib/shoulda/matchers/active_model/validation_matcher.rb +7 -7
  43. data/lib/shoulda/matchers/active_model/validation_message_finder.rb +1 -1
  44. data/lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb +5 -5
  45. data/lib/shoulda/matchers/active_record/association_matcher.rb +3 -3
  46. data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +11 -7
  47. data/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +2 -2
  48. data/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb +5 -5
  49. data/lib/shoulda/matchers/active_record/query_the_database_matcher.rb +5 -2
  50. data/lib/shoulda/matchers/active_record/serialize_matcher.rb +3 -3
  51. data/lib/shoulda/matchers/assertion_error.rb +4 -1
  52. data/lib/shoulda/matchers/independent/delegate_matcher.rb +17 -7
  53. data/lib/shoulda/matchers/integrations/rspec.rb +19 -33
  54. data/lib/shoulda/matchers/version.rb +1 -1
  55. data/shoulda-matchers.gemspec +6 -4
  56. data/spec/shoulda/matchers/action_controller/assign_to_matcher_spec.rb +66 -0
  57. data/spec/shoulda/matchers/action_controller/filter_param_matcher_spec.rb +22 -0
  58. data/spec/shoulda/matchers/action_controller/redirect_to_matcher_spec.rb +42 -0
  59. data/spec/shoulda/matchers/action_controller/render_template_matcher_spec.rb +78 -0
  60. data/spec/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb +58 -0
  61. data/spec/shoulda/matchers/action_controller/respond_with_content_type_matcher_spec.rb +31 -0
  62. data/spec/shoulda/matchers/action_controller/respond_with_matcher_spec.rb +31 -0
  63. data/spec/shoulda/matchers/action_controller/route_matcher_spec.rb +65 -0
  64. data/spec/shoulda/matchers/action_controller/set_session_matcher_spec.rb +51 -0
  65. data/spec/shoulda/matchers/action_controller/set_the_flash_matcher_spec.rb +153 -0
  66. data/spec/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb +142 -0
  67. data/spec/shoulda/matchers/action_mailer/have_sent_email_spec.rb +324 -0
  68. data/spec/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb +111 -0
  69. data/spec/shoulda/matchers/active_model/allow_value_matcher_spec.rb +124 -0
  70. data/spec/shoulda/matchers/active_model/disallow_value_matcher_spec.rb +43 -0
  71. data/spec/shoulda/matchers/active_model/ensure_exclusion_of_matcher_spec.rb +74 -0
  72. data/spec/shoulda/matchers/active_model/ensure_inclusion_of_matcher_spec.rb +171 -0
  73. data/spec/shoulda/matchers/active_model/ensure_length_of_matcher_spec.rb +113 -0
  74. data/spec/shoulda/{active_model → matchers/active_model}/exception_message_finder_spec.rb +2 -2
  75. data/spec/shoulda/matchers/active_model/helpers_spec.rb +158 -0
  76. data/spec/shoulda/matchers/active_model/only_integer_matcher_spec.rb +52 -0
  77. data/spec/shoulda/matchers/active_model/validate_acceptance_of_matcher_spec.rb +41 -0
  78. data/spec/shoulda/matchers/active_model/validate_confirmation_of_matcher_spec.rb +41 -0
  79. data/spec/shoulda/matchers/active_model/validate_format_of_matcher_spec.rb +75 -0
  80. data/spec/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +73 -0
  81. data/spec/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb +127 -0
  82. data/spec/shoulda/matchers/active_model/validate_uniqueness_of_matcher_spec.rb +175 -0
  83. data/spec/shoulda/{active_model → matchers/active_model}/validation_message_finder_spec.rb +0 -1
  84. data/spec/shoulda/matchers/active_record/accept_nested_attributes_for_matcher_spec.rb +106 -0
  85. data/spec/shoulda/matchers/active_record/association_matcher_spec.rb +534 -0
  86. data/spec/shoulda/matchers/active_record/have_db_column_matcher_spec.rb +111 -0
  87. data/spec/shoulda/matchers/active_record/have_db_index_matcher_spec.rb +78 -0
  88. data/spec/shoulda/matchers/active_record/have_readonly_attributes_matcher_spec.rb +41 -0
  89. data/spec/shoulda/{active_record → matchers/active_record}/query_the_database_matcher_spec.rb +10 -10
  90. data/spec/shoulda/matchers/active_record/serialize_matcher_spec.rb +86 -0
  91. data/spec/shoulda/{independent → matchers/independent}/delegate_matcher_spec.rb +55 -34
  92. data/spec/spec_helper.rb +1 -2
  93. data/spec/support/active_model_versions.rb +4 -0
  94. data/spec/support/activemodel_helpers.rb +19 -0
  95. data/spec/support/controller_builder.rb +38 -12
  96. data/spec/support/model_builder.rb +8 -2
  97. metadata +106 -98
  98. data/spec/fixtures/addresses.yml +0 -3
  99. data/spec/fixtures/friendships.yml +0 -0
  100. data/spec/fixtures/posts.yml +0 -5
  101. data/spec/fixtures/products.yml +0 -0
  102. data/spec/fixtures/taggings.yml +0 -0
  103. data/spec/fixtures/tags.yml +0 -9
  104. data/spec/fixtures/users.yml +0 -6
  105. data/spec/shoulda/action_controller/assign_to_matcher_spec.rb +0 -63
  106. data/spec/shoulda/action_controller/filter_param_matcher_spec.rb +0 -20
  107. data/spec/shoulda/action_controller/redirect_to_matcher_spec.rb +0 -40
  108. data/spec/shoulda/action_controller/render_template_matcher_spec.rb +0 -69
  109. data/spec/shoulda/action_controller/render_with_layout_matcher_spec.rb +0 -55
  110. data/spec/shoulda/action_controller/respond_with_content_type_matcher_spec.rb +0 -28
  111. data/spec/shoulda/action_controller/respond_with_matcher_spec.rb +0 -83
  112. data/spec/shoulda/action_controller/route_matcher_spec.rb +0 -65
  113. data/spec/shoulda/action_controller/set_session_matcher_spec.rb +0 -46
  114. data/spec/shoulda/action_controller/set_the_flash_matcher_spec.rb +0 -130
  115. data/spec/shoulda/action_mailer/have_sent_email_spec.rb +0 -333
  116. data/spec/shoulda/active_model/allow_mass_assignment_of_matcher_spec.rb +0 -115
  117. data/spec/shoulda/active_model/allow_value_matcher_spec.rb +0 -131
  118. data/spec/shoulda/active_model/disallow_value_matcher_spec.rb +0 -65
  119. data/spec/shoulda/active_model/ensure_exclusion_of_matcher_spec.rb +0 -79
  120. data/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb +0 -181
  121. data/spec/shoulda/active_model/ensure_length_of_matcher_spec.rb +0 -138
  122. data/spec/shoulda/active_model/helpers_spec.rb +0 -129
  123. data/spec/shoulda/active_model/only_integer_matcher_spec.rb +0 -69
  124. data/spec/shoulda/active_model/validate_acceptance_of_matcher_spec.rb +0 -43
  125. data/spec/shoulda/active_model/validate_confirmation_of_matcher_spec.rb +0 -48
  126. data/spec/shoulda/active_model/validate_format_of_matcher_spec.rb +0 -79
  127. data/spec/shoulda/active_model/validate_numericality_of_matcher_spec.rb +0 -112
  128. data/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb +0 -135
  129. data/spec/shoulda/active_model/validate_uniqueness_of_matcher_spec.rb +0 -154
  130. data/spec/shoulda/active_record/accept_nested_attributes_for_matcher_spec.rb +0 -84
  131. data/spec/shoulda/active_record/association_matcher_spec.rb +0 -642
  132. data/spec/shoulda/active_record/have_db_column_matcher_spec.rb +0 -185
  133. data/spec/shoulda/active_record/have_db_index_matcher_spec.rb +0 -105
  134. data/spec/shoulda/active_record/have_readonly_attributes_matcher_spec.rb +0 -46
  135. data/spec/shoulda/active_record/serialize_matcher_spec.rb +0 -81
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActionController::FilterParamMatcher do
4
+ it 'accepts filtering a filtered parameter' do
5
+ filter(:secret)
6
+
7
+ nil.should filter_param(:secret)
8
+ end
9
+
10
+ it 'rejects filtering an unfiltered parameter' do
11
+ filter(:secret)
12
+ matcher = filter_param(:other)
13
+
14
+ matcher.matches?(nil).should be_false
15
+
16
+ matcher.failure_message_for_should.should =~ /Expected other to be filtered.*secret/
17
+ end
18
+
19
+ def filter(param)
20
+ Rails.application.config.filter_parameters = [param]
21
+ end
22
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActionController::RedirectToMatcher do
4
+ context 'a controller that redirects' do
5
+ it 'accepts redirecting to that url' do
6
+ controller_redirecting_to('/some/url').should redirect_to('/some/url')
7
+ end
8
+
9
+ it 'rejects redirecting to a different url' do
10
+ controller_redirecting_to('/some/url').
11
+ should_not redirect_to('/some/other/url')
12
+ end
13
+
14
+ it 'accepts redirecting to that url in a block' do
15
+ controller_redirecting_to('/some/url').
16
+ should redirect_to('somewhere') { '/some/url' }
17
+ end
18
+
19
+ it 'rejects redirecting to a different url in a block' do
20
+ controller_redirecting_to('/some/url').
21
+ should_not redirect_to('somewhere else') { '/some/other/url' }
22
+ end
23
+
24
+ def controller_redirecting_to(url)
25
+ build_response { redirect_to url }
26
+ end
27
+ end
28
+
29
+ context 'a controller that does not redirect' do
30
+ it 'rejects redirecting to a url' do
31
+ controller = build_response { render :text => 'hello' }
32
+
33
+ controller.should_not redirect_to('/some/url')
34
+ end
35
+ end
36
+
37
+ it 'provides the correct description when provided a block' do
38
+ matcher = redirect_to('somewhere else') { '/some/other/url' }
39
+
40
+ matcher.description.should == 'redirect to somewhere else'
41
+ end
42
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActionController::RenderTemplateMatcher do
4
+ include ActionController::TemplateAssertions
5
+
6
+ context 'a controller that renders a template' do
7
+ it 'accepts rendering that template' do
8
+ controller_with_show.should render_template(:show)
9
+ end
10
+
11
+ it 'rejects rendering a different template' do
12
+ controller_with_show.should_not render_template(:index)
13
+ end
14
+
15
+ it 'accepts rendering that template in the given context' do
16
+ controller_with_show.should render_template(:show).in_context(self)
17
+ end
18
+
19
+ it 'rejects rendering a different template in the given context' do
20
+ controller_with_show.should_not render_template(:index).in_context(self)
21
+ end
22
+
23
+ def controller_with_show
24
+ build_response(:action => 'show') { render }
25
+ end
26
+ end
27
+
28
+ context 'a controller that renders a partial' do
29
+ it 'accepts rendering that partial' do
30
+ controller_with_customer_partial.
31
+ should render_template(:partial => '_customer')
32
+ end
33
+
34
+ it 'rejects rendering a different template' do
35
+ controller_with_customer_partial.
36
+ should_not render_template(:partial => '_client')
37
+ end
38
+
39
+ it 'accepts rendering that template in the given context' do
40
+ controller_with_customer_partial.
41
+ should render_template(:partial => '_customer').in_context(self)
42
+ end
43
+
44
+ it 'rejects rendering a different template in the given context' do
45
+ controller_with_customer_partial.
46
+ should_not render_template(:partial => '_client').in_context(self)
47
+ end
48
+
49
+ def controller_with_customer_partial
50
+ build_response(:partial => '_customer') { render :partial => 'customer' }
51
+ end
52
+ end
53
+
54
+ context 'a controller that does not render partials' do
55
+ it 'accepts not rendering a partial' do
56
+ controller = build_response(:action => 'show') { render }
57
+
58
+ controller.should render_template(:partial => false)
59
+ end
60
+ end
61
+
62
+ context 'a controller that renders a partial several times' do
63
+ it 'accepts rendering that partial twice' do
64
+ controller = build_response(:partial => '_customer') do
65
+ render :partial => 'customer', :collection => [1,2]
66
+ end
67
+
68
+ controller.should render_template(:partial => '_customer', :count => 2)
69
+ end
70
+ end
71
+
72
+ context 'a controller that does not render a template' do
73
+ it 'rejects rendering a template' do
74
+ build_response { render :nothing => true }.
75
+ should_not render_template(:show)
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActionController::RenderWithLayoutMatcher do
4
+ include ActionController::TemplateAssertions
5
+
6
+ context 'a controller that renders with a layout' do
7
+ it 'accepts rendering with any layout' do
8
+ controller_with_wide_layout.should render_with_layout
9
+ end
10
+
11
+ it 'accepts rendering with that layout' do
12
+ controller_with_wide_layout.should render_with_layout(:wide)
13
+ end
14
+
15
+ it 'rejects rendering with another layout' do
16
+ controller_with_wide_layout.should_not render_with_layout(:other)
17
+ end
18
+
19
+ def controller_with_wide_layout
20
+ create_view('layouts/wide.html.erb', 'some content, <%= yield %>')
21
+ build_response { render :layout => 'wide' }
22
+ end
23
+ end
24
+
25
+ context 'a controller that renders without a layout' do
26
+
27
+ it 'rejects rendering with a layout' do
28
+ controller_without_layout = build_response { render :layout => false }
29
+
30
+ controller_without_layout.should_not render_with_layout
31
+ end
32
+ end
33
+
34
+ context 'a controller that renders a partial' do
35
+ it 'rejects rendering with a layout' do
36
+ controller_with_partial = build_response { render :partial => 'partial' }
37
+
38
+ controller_with_partial.should_not render_with_layout
39
+ end
40
+ end
41
+
42
+ context 'given a context with layouts' do
43
+ it 'accepts that layout in that context' do
44
+ set_in_context_layout('happy')
45
+
46
+ controller_without_layout.should render_with_layout('happy').in_context(self)
47
+ end
48
+
49
+ def set_in_context_layout(layout)
50
+ @layouts = Hash.new(0)
51
+ @layouts[layout] = 1
52
+ end
53
+
54
+ def controller_without_layout
55
+ build_response { render :layout => false }
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActionController::RespondWithContentTypeMatcher do
4
+ it 'generates the correct description' do
5
+ expected = 'respond with content type of application/xml'
6
+
7
+ respond_with_content_type(:xml).description.should == expected
8
+ end
9
+
10
+ it 'accepts responding with content type as symbol' do
11
+ xml_controller.should respond_with_content_type(:xml)
12
+ end
13
+
14
+ it 'accepts responding with qualified MIME-style content type' do
15
+ xml_controller.should respond_with_content_type('application/xml')
16
+ end
17
+
18
+ it 'accepts responding with a regex matching the content type' do
19
+ xml_controller.should respond_with_content_type(/xml/)
20
+ end
21
+
22
+ it 'rejects responding with another content type' do
23
+ xml_controller.should_not respond_with_content_type(:json)
24
+ end
25
+
26
+ def xml_controller
27
+ build_response do
28
+ render :xml => { :user => 'thoughtbot' }.to_xml
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActionController::RespondWithMatcher do
4
+ statuses = { :success => 200, :redirect => 301, :missing => 404, :error => 500,
5
+ :not_implemented => 501 }
6
+
7
+ statuses.each do |human_name, numeric_code|
8
+ context "a controller responding with #{human_name}" do
9
+ it 'accepts responding with a numeric response code' do
10
+ controller_with_status(numeric_code).should respond_with(numeric_code)
11
+ end
12
+
13
+ it 'accepts responding with a symbol response code' do
14
+ controller_with_status(numeric_code).should respond_with(human_name)
15
+ end
16
+
17
+ it 'rejects responding with another status' do
18
+ another_status = statuses.except(human_name).keys.first
19
+
20
+ controller_with_status(numeric_code).
21
+ should_not respond_with(another_status)
22
+ end
23
+ end
24
+ end
25
+
26
+ def controller_with_status(status)
27
+ build_response do
28
+ render :text => 'text', :status => status
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActionController::RouteMatcher do
4
+ context 'given a controller with a defined glob url' do
5
+ it 'accepts glob route' do
6
+ controller = define_controller('Examples').new
7
+
8
+ define_routes do
9
+ match 'examples/*id', :to => 'examples#example'
10
+ end
11
+
12
+ controller.should route(:get, '/examples/foo/bar').
13
+ to(:action => 'example', :id => 'foo/bar')
14
+ end
15
+ end
16
+
17
+ context 'given a controller with a defined route' do
18
+
19
+ it 'accepts routing the correct path to the correct parameters' do
20
+ route_examples_to_examples.should route(:get, '/examples/1').
21
+ to(:action => 'example', :id => '1')
22
+ end
23
+
24
+ it 'accepts a symbol controller' do
25
+ route_examples_to_examples
26
+ Object.new.should route(:get, '/examples/1').
27
+ to(:controller => :examples, :action => 'example', :id => '1')
28
+ end
29
+
30
+ it 'accepts a symbol action' do
31
+ route_examples_to_examples.should route(:get, '/examples/1').
32
+ to(:action => :example, :id => '1')
33
+ end
34
+
35
+ it 'accepts a non-string parameter' do
36
+ route_examples_to_examples.should route(:get, '/examples/1').
37
+ to(:action => 'example', :id => 1)
38
+ end
39
+
40
+ it 'rejects an undefined route' do
41
+ route_examples_to_examples.
42
+ should_not route(:get, '/bad_route').to(:var => 'value')
43
+ end
44
+
45
+ it 'rejects a route for another controller' do
46
+ route_examples_to_examples
47
+ other = define_controller('Other').new
48
+ other.should_not route(:get, '/examples/1').
49
+ to(:action => 'example', :id => '1')
50
+ end
51
+
52
+ it 'rejects a route for different parameters' do
53
+ route_examples_to_examples.should_not route(:get, '/examples/1').
54
+ to(:action => 'other', :id => '1')
55
+ end
56
+
57
+ def route_examples_to_examples
58
+ define_routes do
59
+ get 'examples/:id', :to => 'examples#example'
60
+ end
61
+
62
+ define_controller('Examples').new
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActionController::SetSessionMatcher do
4
+ context 'a controller that sets a session variable' do
5
+ it 'accepts assigning to that variable' do
6
+ controller_with_session(:var => 'hi').should set_session(:var)
7
+ end
8
+
9
+ it 'accepts assigning the correct value to that variable' do
10
+ controller_with_session(:var => 'hi').should set_session(:var).to('hi')
11
+ end
12
+
13
+ it 'rejects assigning another value to that variable' do
14
+ controller_with_session(:var => 'hi').should_not set_session(:var).to('other')
15
+ end
16
+
17
+ it 'rejects assigning to another variable' do
18
+ controller_with_session(:var => 'hi').should_not set_session(:other)
19
+ end
20
+
21
+ it 'accepts assigning nil to another variable' do
22
+ controller_with_session(:var => 'hi').should set_session(:other).to(nil)
23
+ end
24
+
25
+ it 'accepts assigning false to that variable' do
26
+ controller_with_session(:var => false).should set_session(:var).to(false)
27
+ end
28
+
29
+ it 'accepts assigning to the same value in the test context' do
30
+ expected = 'value'
31
+
32
+ controller_with_session(:var => expected).
33
+ should set_session(:var).in_context(self).to { expected }
34
+ end
35
+
36
+ it 'rejects assigning to the another value in the test context' do
37
+ expected = 'other'
38
+
39
+ controller_with_session(:var => 'unexpected').
40
+ should_not set_session(:var).in_context(self).to { expected }
41
+ end
42
+
43
+ def controller_with_session(session_hash)
44
+ build_response do
45
+ session_hash.each do |key, value|
46
+ session[key] = value
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,153 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActionController::SetTheFlashMatcher do
4
+ it 'fails with unmatchable #to' do
5
+ expect { set_the_flash.to(1) }.to raise_error('cannot match against 1')
6
+ end
7
+
8
+ context 'a controller that sets a flash message' do
9
+ it 'accepts setting any flash message' do
10
+ controller_with_flash(:notice => 'hi').should set_the_flash
11
+ end
12
+
13
+ it 'accepts setting the exact flash message' do
14
+ controller_with_flash(:notice => 'hi').should set_the_flash.to('hi')
15
+ end
16
+
17
+ it 'accepts setting a matched flash message' do
18
+ controller_with_flash(:notice => 'hello').should set_the_flash.to(/he/)
19
+ end
20
+
21
+ it 'rejects setting a different flash message' do
22
+ controller_with_flash(:notice => 'hi').
23
+ should_not set_the_flash.to('other')
24
+ end
25
+
26
+ it 'rejects setting a different pattern' do
27
+ controller_with_flash(:notice => 'hi').
28
+ should_not set_the_flash.to(/other/)
29
+ end
30
+ end
31
+
32
+ context 'a controller that sets a flash.now message' do
33
+ it 'rejects setting any flash message' do
34
+ controller_with_flash_now.should_not set_the_flash
35
+ end
36
+
37
+ it 'accepts setting any flash.now message' do
38
+ controller_with_flash_now.should set_the_flash.now
39
+ end
40
+
41
+ it 'accepts setting the exact flash.now message' do
42
+ controller_with_flash_now(:notice => 'hi').
43
+ should set_the_flash.now.to('hi')
44
+ end
45
+
46
+ it 'accepts setting a matched flash.now message' do
47
+ controller_with_flash_now(:notice => 'flasher').
48
+ should set_the_flash.now.to(/lash/)
49
+ end
50
+
51
+ it 'rejects setting a different flash.now message' do
52
+ controller_with_flash_now(:notice => 'hi').
53
+ should_not set_the_flash.now.to('other')
54
+ end
55
+
56
+ it 'rejects setting a different flash.now pattern' do
57
+ controller_with_flash_now(:notice => 'hi').
58
+ should_not set_the_flash.now.to(/other/)
59
+ end
60
+ end
61
+
62
+ context 'a controller that sets flash messages for multiple keys' do
63
+ it 'accepts flash message for either key' do
64
+ controller = controller_with_flash(:notice => 'one', :alert => 'two')
65
+
66
+ controller.should set_the_flash[:notice]
67
+ controller.should set_the_flash[:alert]
68
+ end
69
+
70
+ it 'rejects a flash message that is not one of the set keys' do
71
+ controller_with_flash(:notice => 'one', :alert => 'two').
72
+ should_not set_the_flash[:warning]
73
+ end
74
+
75
+ it 'accepts exact flash message of notice' do
76
+ controller_with_flash(:notice => 'one', :alert => 'two').
77
+ should set_the_flash[:notice].to('one')
78
+ end
79
+
80
+ it 'accepts setting a matched flash message of notice' do
81
+ controller_with_flash(:notice => 'one', :alert => 'two').
82
+ should set_the_flash[:notice].to(/on/)
83
+ end
84
+
85
+ it 'rejects setting a different flash message of notice' do
86
+ controller_with_flash(:notice => 'one', :alert => 'two').
87
+ should_not set_the_flash[:notice].to('other')
88
+ end
89
+
90
+ it 'rejects setting a different pattern' do
91
+ controller_with_flash(:notice => 'one', :alert => 'two').
92
+ should_not set_the_flash[:notice].to(/other/)
93
+ end
94
+ end
95
+
96
+ context 'a controller that sets flash and flash.now' do
97
+ it 'accepts setting any flash.now message' do
98
+ controller = build_response do
99
+ flash.now[:notice] = 'value'
100
+ flash[:success] = 'great job'
101
+ end
102
+
103
+ controller.should set_the_flash.now
104
+ controller.should set_the_flash
105
+ end
106
+
107
+ it 'accepts setting a matched flash.now message' do
108
+ controller = build_response do
109
+ flash.now[:notice] = 'value'
110
+ flash[:success] = 'great job'
111
+ end
112
+
113
+ controller.should set_the_flash.now.to(/value/)
114
+ controller.should set_the_flash.to(/great/)
115
+ end
116
+
117
+ it 'rejects setting a different flash.now message' do
118
+ controller = build_response do
119
+ flash.now[:notice] = 'value'
120
+ flash[:success] = 'great job'
121
+ end
122
+
123
+ controller.should_not set_the_flash.now.to('other')
124
+ controller.should_not set_the_flash.to('other')
125
+ end
126
+ end
127
+
128
+ context 'a controller that does not set a flash message' do
129
+ it 'rejects setting any flash message' do
130
+ controller_with_no_flashes.should_not set_the_flash
131
+ end
132
+ end
133
+
134
+ def controller_with_no_flashes
135
+ build_response
136
+ end
137
+
138
+ def controller_with_flash(flash_hash)
139
+ build_response do
140
+ flash_hash.each do |key, value|
141
+ flash[key] = value
142
+ end
143
+ end
144
+ end
145
+
146
+ def controller_with_flash_now(flash_hash = { :notice => 'hi' })
147
+ build_response do
148
+ flash_hash.each do |key, value|
149
+ flash.now[key] = value
150
+ end
151
+ end
152
+ end
153
+ end