shoulda 2.9.1 → 2.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/README.rdoc +27 -5
  2. data/Rakefile +2 -2
  3. data/lib/shoulda.rb +1 -1
  4. data/lib/shoulda/{controller.rb → action_controller.rb} +6 -8
  5. data/lib/shoulda/{controller → action_controller}/helpers.rb +1 -16
  6. data/lib/shoulda/action_controller/macros.rb +277 -0
  7. data/lib/shoulda/action_controller/matchers.rb +37 -0
  8. data/lib/shoulda/action_controller/matchers/assign_to_matcher.rb +109 -0
  9. data/lib/shoulda/action_controller/matchers/filter_param_matcher.rb +57 -0
  10. data/lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb +81 -0
  11. data/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb +70 -0
  12. data/lib/shoulda/action_controller/matchers/respond_with_matcher.rb +81 -0
  13. data/lib/shoulda/action_controller/matchers/route_matcher.rb +93 -0
  14. data/lib/shoulda/action_controller/matchers/set_session_matcher.rb +83 -0
  15. data/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb +85 -0
  16. data/lib/shoulda/action_view.rb +10 -0
  17. data/lib/shoulda/action_view/macros.rb +56 -0
  18. data/lib/shoulda/active_record/macros.rb +8 -13
  19. data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +1 -1
  20. data/lib/shoulda/rails.rb +4 -3
  21. data/lib/shoulda/rspec.rb +7 -5
  22. data/test/functional/posts_controller_test.rb +28 -22
  23. data/test/functional/users_controller_test.rb +0 -19
  24. data/test/matchers/{allow_mass_assignment_of_matcher_test.rb → active_record/allow_mass_assignment_of_matcher_test.rb} +1 -1
  25. data/test/matchers/{allow_value_matcher_test.rb → active_record/allow_value_matcher_test.rb} +1 -1
  26. data/test/matchers/{association_matcher_test.rb → active_record/association_matcher_test.rb} +1 -1
  27. data/test/matchers/{ensure_inclusion_of_matcher_test.rb → active_record/ensure_inclusion_of_matcher_test.rb} +1 -1
  28. data/test/matchers/{ensure_length_of_matcher_test.rb → active_record/ensure_length_of_matcher_test.rb} +1 -1
  29. data/test/matchers/{have_db_column_matcher_test.rb → active_record/have_db_column_matcher_test.rb} +1 -1
  30. data/test/matchers/{have_index_matcher_test.rb → active_record/have_index_matcher_test.rb} +1 -1
  31. data/test/matchers/{have_named_scope_matcher_test.rb → active_record/have_named_scope_matcher_test.rb} +1 -1
  32. data/test/matchers/{have_readonly_attributes_matcher_test.rb → active_record/have_readonly_attributes_matcher_test.rb} +1 -1
  33. data/test/matchers/{validate_acceptance_of_matcher_test.rb → active_record/validate_acceptance_of_matcher_test.rb} +1 -1
  34. data/test/matchers/{validate_numericality_of_matcher_test.rb → active_record/validate_numericality_of_matcher_test.rb} +1 -1
  35. data/test/matchers/{validate_presence_of_matcher_test.rb → active_record/validate_presence_of_matcher_test.rb} +1 -1
  36. data/test/matchers/{validate_uniqueness_of_matcher_test.rb → active_record/validate_uniqueness_of_matcher_test.rb} +8 -2
  37. data/test/matchers/controller/assign_to_matcher_test.rb +35 -0
  38. data/test/matchers/controller/filter_param_matcher_test.rb +32 -0
  39. data/test/matchers/controller/render_with_layout_matcher_test.rb +33 -0
  40. data/test/matchers/controller/respond_with_content_type_matcher_test.rb +27 -0
  41. data/test/matchers/controller/respond_with_matcher_test.rb +106 -0
  42. data/test/matchers/controller/route_matcher_test.rb +58 -0
  43. data/test/matchers/controller/set_session_matcher_test.rb +27 -0
  44. data/test/matchers/controller/set_the_flash_matcher.rb +41 -0
  45. data/test/model_builder.rb +47 -2
  46. data/test/rails_root/app/models/user.rb +2 -1
  47. data/test/rails_root/config/environment.rb +1 -1
  48. data/test/rspec_test.rb +207 -0
  49. data/test/unit/user_test.rb +10 -1
  50. metadata +38 -22
  51. data/lib/shoulda/controller/formats/html.rb +0 -199
  52. data/lib/shoulda/controller/formats/xml.rb +0 -168
  53. data/lib/shoulda/controller/macros.rb +0 -336
  54. data/lib/shoulda/controller/resource_options.rb +0 -233
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', 'test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
2
 
3
3
  class AssociationMatcherTest < Test::Unit::TestCase # :nodoc:
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', 'test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
2
 
3
3
  class EnsureInclusionOfMatcherTest < Test::Unit::TestCase # :nodoc:
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', 'test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
2
 
3
3
  class EnsureLengthOfMatcher < Test::Unit::TestCase # :nodoc:
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', 'test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
2
 
3
3
  class HaveDbColumnMatcherTest < Test::Unit::TestCase # :nodoc:
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', 'test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
2
 
3
3
  class HaveIndexMatcherTest < Test::Unit::TestCase # :nodoc:
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', 'test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
2
 
3
3
  class HaveNamedScopeMatcherTest < Test::Unit::TestCase # :nodoc:
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', 'test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
2
 
3
3
  class HaveReadonlyAttributesMatcherTest < Test::Unit::TestCase # :nodoc:
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', 'test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
2
 
3
3
  class ValidateAcceptanceOfMatcherTest < Test::Unit::TestCase # :nodoc:
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', 'test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
2
 
3
3
  class ValidateNumericalityOfMatcherTest < Test::Unit::TestCase # :nodoc:
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', 'test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
2
 
3
3
  class ValidatePresenceOfMatcherTest < Test::Unit::TestCase # :nodoc:
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', 'test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
2
 
3
3
  class ValidateUniquenessOfMatcherTest < Test::Unit::TestCase # :nodoc:
4
4
 
@@ -31,10 +31,16 @@ class ValidateUniquenessOfMatcherTest < Test::Unit::TestCase # :nodoc:
31
31
  context "without an existing value" do
32
32
  setup do
33
33
  assert_nil Example.find(:first)
34
+ @matcher = validate_uniqueness_of(:attr)
34
35
  end
35
36
 
36
37
  should "fail to require a unique value" do
37
- assert_rejects validate_uniqueness_of(:attr), @model
38
+ assert_rejects @matcher, @model
39
+ end
40
+
41
+ should "alert the tester that an existing value is not present" do
42
+ @matcher.matches?(@model)
43
+ assert @matcher.negative_failure_message =~ /^Can't find first .*/
38
44
  end
39
45
  end
40
46
  end
@@ -0,0 +1,35 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class AssignToMatcherTest < Test::Unit::TestCase # :nodoc:
4
+
5
+ context "a controller that assigns to an instance variable" do
6
+ setup do
7
+ @controller = build_response { @var = 'value' }
8
+ end
9
+
10
+ should "accept assigning to that variable" do
11
+ assert_accepts assign_to(:var), @controller
12
+ end
13
+
14
+ should "accept assigning to that variable with the correct class" do
15
+ assert_accepts assign_to(:var).with_kind_of(String), @controller
16
+ end
17
+
18
+ should "reject assigning to that variable with another class" do
19
+ assert_rejects assign_to(:var).with_kind_of(Fixnum), @controller
20
+ end
21
+
22
+ should "accept assigning the correct value to that variable" do
23
+ assert_accepts assign_to(:var).with('value'), @controller
24
+ end
25
+
26
+ should "reject assigning another value to that variable" do
27
+ assert_rejects assign_to(:var).with('other'), @controller
28
+ end
29
+
30
+ should "reject assigning to another variable" do
31
+ assert_rejects assign_to(:other), @controller
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,32 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class FilterParamMatcherTest < Test::Unit::TestCase # :nodoc:
4
+
5
+ context "a controller that filters no parameters" do
6
+ setup do
7
+ @controller = define_controller(:examples).new
8
+ end
9
+
10
+ should "reject filtering any parameter" do
11
+ assert_rejects filter_param(:any), @controller
12
+ end
13
+ end
14
+
15
+ context "a controller that filters a parameter" do
16
+ setup do
17
+ @controller = define_controller :examples do
18
+ filter_parameter_logging :password
19
+ end.new
20
+ end
21
+
22
+ should "accept filtering that parameter" do
23
+ assert_accepts filter_param(:password), @controller
24
+ end
25
+
26
+ should "reject filtering another parameter" do
27
+ assert_rejects filter_param(:other), @controller
28
+ end
29
+ end
30
+
31
+ end
32
+
@@ -0,0 +1,33 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class RenderWithLayoutMatcherTest < Test::Unit::TestCase # :nodoc:
4
+
5
+ context "a controller that renders with a layout" do
6
+ setup do
7
+ @controller = build_response { render :layout => 'wide' }
8
+ end
9
+
10
+ should "accept rendering with any layout" do
11
+ assert_accepts render_with_layout, @controller
12
+ end
13
+
14
+ should "accept rendering with that layout" do
15
+ assert_accepts render_with_layout(:wide), @controller
16
+ end
17
+
18
+ should "reject rendering with another layout" do
19
+ assert_rejects render_with_layout(:other), @controller
20
+ end
21
+ end
22
+
23
+ context "a controller that renders without a layout" do
24
+ setup do
25
+ @controller = build_response { render :layout => false }
26
+ end
27
+
28
+ should "reject rendering with a layout" do
29
+ assert_rejects render_with_layout, @controller
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,27 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class RespondWithContentTypeMatcherTest < Test::Unit::TestCase # :nodoc:
4
+
5
+ context "a controller responding with content type :xml" do
6
+ setup do
7
+ @controller = build_response { render :xml => { :user => "thoughtbot" }.to_xml }
8
+ end
9
+
10
+ should "accept responding with content type :xml" do
11
+ assert_accepts respond_with_content_type(:xml), @controller
12
+ end
13
+
14
+ should "accept responding with content type 'application/xml'" do
15
+ assert_accepts respond_with_content_type('application/xml'), @controller
16
+ end
17
+
18
+ should "accept responding with content type /xml/" do
19
+ assert_accepts respond_with_content_type(/xml/), @controller
20
+ end
21
+
22
+ should "reject responding with another content type" do
23
+ assert_rejects respond_with_content_type(:json), @controller
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,106 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class RespondWithMatcherTest < Test::Unit::TestCase # :nodoc:
4
+
5
+ context "a controller responding with success" do
6
+ setup do
7
+ @controller = build_response { render :text => "text", :status => 200 }
8
+ end
9
+
10
+ should "accept responding with 200" do
11
+ assert_accepts respond_with(200), @controller
12
+ end
13
+
14
+ should "accept responding with :success" do
15
+ assert_accepts respond_with(:success), @controller
16
+ end
17
+
18
+ should "reject responding with another status" do
19
+ assert_rejects respond_with(:error), @controller
20
+ end
21
+ end
22
+
23
+ context "a controller responding with redirect" do
24
+ setup do
25
+ @controller = build_response { render :text => "text", :status => 301 }
26
+ end
27
+
28
+ should "accept responding with 301" do
29
+ assert_accepts respond_with(301), @controller
30
+ end
31
+
32
+ should "accept responding with :redirect" do
33
+ assert_accepts respond_with(:redirect), @controller
34
+ end
35
+
36
+ should "reject responding with another status" do
37
+ assert_rejects respond_with(:error), @controller
38
+ end
39
+ end
40
+
41
+ context "a controller responding with missing" do
42
+ setup do
43
+ @controller = build_response { render :text => "text", :status => 404 }
44
+ end
45
+
46
+ should "accept responding with 404" do
47
+ assert_accepts respond_with(404), @controller
48
+ end
49
+
50
+ should "accept responding with :missing" do
51
+ assert_accepts respond_with(:missing), @controller
52
+ end
53
+
54
+ should "reject responding with another status" do
55
+ assert_rejects respond_with(:success), @controller
56
+ end
57
+ end
58
+
59
+ context "a controller responding with error" do
60
+ setup do
61
+ @controller = build_response { render :text => "text", :status => 500 }
62
+ end
63
+
64
+ should "accept responding with 500" do
65
+ assert_accepts respond_with(500), @controller
66
+ end
67
+
68
+ should "accept responding with :error" do
69
+ assert_accepts respond_with(:error), @controller
70
+ end
71
+
72
+ should "reject responding with another status" do
73
+ assert_rejects respond_with(:success), @controller
74
+ end
75
+ end
76
+
77
+ context "a controller responding with not implemented" do
78
+ setup do
79
+ @controller = build_response { render :text => "text", :status => 501 }
80
+ end
81
+
82
+ should "accept responding with 501" do
83
+ assert_accepts respond_with(501), @controller
84
+ end
85
+
86
+ should "accept responding with :not_implemented" do
87
+ assert_accepts respond_with(:not_implemented), @controller
88
+ end
89
+
90
+ should "reject responding with another status" do
91
+ assert_rejects respond_with(:success), @controller
92
+ end
93
+ end
94
+
95
+ context "a controller raising an error" do
96
+ setup do
97
+ @controller = build_response { raise RailsError }
98
+ end
99
+
100
+ should "reject responding with any status" do
101
+ assert_rejects respond_with(:success), @controller
102
+ end
103
+ end
104
+
105
+ end
106
+
@@ -0,0 +1,58 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class RouteToMatcherTest < Test::Unit::TestCase # :nodoc:
4
+
5
+ context "given a controller with a defined route" do
6
+ setup do
7
+ @controller = define_controller('Examples').new
8
+ define_routes do |map|
9
+ map.connect 'examples/:id', :controller => 'examples',
10
+ :action => 'example'
11
+ end
12
+ end
13
+
14
+ should "accept routing the correct path to the correct parameters" do
15
+ assert_accepts route(:get, '/examples/1').
16
+ to(:action => 'example', :id => '1'),
17
+ @controller
18
+ end
19
+
20
+ should "accept a symbol controller" do
21
+ assert_accepts route(:get, '/examples/1').
22
+ to(:controller => :examples,
23
+ :action => 'example',
24
+ :id => '1'),
25
+ self
26
+ end
27
+
28
+ should "accept a symbol action" do
29
+ assert_accepts route(:get, '/examples/1').
30
+ to(:action => :example, :id => '1'),
31
+ @controller
32
+ end
33
+
34
+ should "accept a non-string parameter" do
35
+ assert_accepts route(:get, '/examples/1').
36
+ to(:action => 'example', :id => 1),
37
+ @controller
38
+ end
39
+
40
+ should "reject an undefined route" do
41
+ assert_rejects route(:get, '/bad_route').to(:var => 'value'), @controller
42
+ end
43
+
44
+ should "reject a route for another controller" do
45
+ @other = define_controller('Other').new
46
+ assert_rejects route(:get, '/examples/1').
47
+ to(:action => 'example', :id => '1'),
48
+ @other
49
+ end
50
+
51
+ should "reject a route for different parameters" do
52
+ assert_rejects route(:get, '/examples/1').
53
+ to(:action => 'other', :id => '1'),
54
+ @controller
55
+ end
56
+ end
57
+
58
+ end
@@ -0,0 +1,27 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class SetSessionMatcherTest < Test::Unit::TestCase # :nodoc:
4
+
5
+ context "a controller that sets a session variable" do
6
+ setup do
7
+ @controller = build_response { session[:var] = 'value' }
8
+ end
9
+
10
+ should "accept assigning to that variable" do
11
+ assert_accepts set_session(:var), @controller
12
+ end
13
+
14
+ should "accept assigning the correct value to that variable" do
15
+ assert_accepts set_session(:var).to('value'), @controller
16
+ end
17
+
18
+ should "reject assigning another value to that variable" do
19
+ assert_rejects set_session(:var).to('other'), @controller
20
+ end
21
+
22
+ should "reject assigning to another variable" do
23
+ assert_rejects set_session(:other), @controller
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,41 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class SetTheFlashMatcherTest < Test::Unit::TestCase # :nodoc:
4
+
5
+ context "a controller that sets a flash message" do
6
+ setup do
7
+ @controller = build_response { flash[:notice] = 'value' }
8
+ end
9
+
10
+ should "accept setting any flash message" do
11
+ assert_accepts set_the_flash, @controller
12
+ end
13
+
14
+ should "accept setting the exact flash message" do
15
+ assert_accepts set_the_flash.to('value'), @controller
16
+ end
17
+
18
+ should "accept setting a matched flash message" do
19
+ assert_accepts set_the_flash.to(/value/), @controller
20
+ end
21
+
22
+ should "reject setting a different flash message" do
23
+ assert_rejects set_the_flash.to('other'), @controller
24
+ end
25
+
26
+ should "reject setting a different pattern" do
27
+ assert_rejects set_the_flash.to(/other/), @controller
28
+ end
29
+ end
30
+
31
+ context "a controller that doesn't set a flash message" do
32
+ setup do
33
+ @controller = build_response
34
+ end
35
+
36
+ should "reject setting any flash message" do
37
+ assert_rejects set_the_flash, @controller
38
+ end
39
+ end
40
+
41
+ end