Commit 867da600ce2e197b1c4c1cd89e9899802fb073ee

Authored by Dan Croak
1 parent 1198ed35

cleaning up shoulda macros

test/shoulda_macros/forms.rb
1 class Test::Unit::TestCase 1 class Test::Unit::TestCase
2 - def self.should_have_form(opts)  
3 - model = self.name.gsub(/ControllerTest$/, '').singularize.downcase  
4 - model = model[model.rindex('::')+2..model.size] if model.include?('::')  
5 - http_method, hidden_http_method = form_http_method opts[:method]  
6 - should "have a #{model} form" do  
7 - assert_select "form[action=?][method=#{http_method}]", eval(opts[:action]) do  
8 - if hidden_http_method  
9 - assert_select "input[type=hidden][name=_method][value=#{hidden_http_method}]"  
10 - end  
11 - opts[:fields].each do |attribute, type|  
12 - attribute = attribute.is_a?(Symbol) ? "#{model}[#{attribute.to_s}]" : attribute  
13 - assert_select "input[type=#{type.to_s}][name=?]", attribute  
14 - end  
15 - assert_select "input[type=submit]"  
16 - end  
17 - end  
18 - end  
19 -  
20 - def self.form_http_method(http_method)  
21 - http_method = http_method.nil? ? 'post' : http_method.to_s  
22 - if http_method == "post" || http_method == "get"  
23 - return http_method, nil  
24 - else  
25 - return "post", http_method  
26 - end  
27 - end  
28 -  
29 # assert_form posts_url, :put do 2 # assert_form posts_url, :put do
30 # assert_text_field :post, :title 3 # assert_text_field :post, :title
31 # assert_text_area :post, :body 4 # assert_text_area :post, :body
@@ -56,26 +29,34 @@ class Test::Unit::TestCase @@ -56,26 +29,34 @@ class Test::Unit::TestCase
56 assert_select "input[type=submit]" 29 assert_select "input[type=submit]"
57 end 30 end
58 31
59 - # TODO: default to test the label, provide :label => false option  
60 - def assert_text_field(model, attribute) 32 + def assert_text_field(model, attribute, opts = {})
  33 + unless opts[:label] == false
  34 + assert_label model, attribute
  35 + end
61 assert_select "input[type=text][name=?]", 36 assert_select "input[type=text][name=?]",
62 "#{model.to_s}[#{attribute.to_s}]" 37 "#{model.to_s}[#{attribute.to_s}]"
63 end 38 end
64 39
65 - # TODO: default to test the label, provide :label => false option  
66 - def assert_text_area(model, attribute) 40 + def assert_text_area(model, attribute, opts = {})
  41 + unless opts[:label] == false
  42 + assert_label model, attribute
  43 + end
67 assert_select "textarea[name=?]", 44 assert_select "textarea[name=?]",
68 "#{model.to_s}[#{attribute.to_s}]" 45 "#{model.to_s}[#{attribute.to_s}]"
69 end 46 end
70 47
71 - # TODO: default to test the label, provide :label => false option  
72 - def assert_password_field(model, attribute) 48 + def assert_password_field(model, attribute, opts = {})
  49 + unless opts[:label] == false
  50 + assert_label model, attribute
  51 + end
73 assert_select "input[type=password][name=?]", 52 assert_select "input[type=password][name=?]",
74 "#{model.to_s}[#{attribute.to_s}]" 53 "#{model.to_s}[#{attribute.to_s}]"
75 end 54 end
76 55
77 - # TODO: default to test the label, provide :label => false option  
78 - def assert_radio_button(model, attribute) 56 + def assert_radio_button(model, attribute, opts = {})
  57 + unless opts[:label] == false
  58 + assert_label model, attribute
  59 + end
79 assert_select "input[type=radio][name=?]", 60 assert_select "input[type=radio][name=?]",
80 "#{model.to_s}[#{attribute.to_s}]" 61 "#{model.to_s}[#{attribute.to_s}]"
81 end 62 end
test/shoulda_macros/pagination.rb
1 class Test::Unit::TestCase 1 class Test::Unit::TestCase
2 # Example: 2 # Example:
3 - # context "a GET to index logged in as admin" do 3 + # context "a GET to index signed in as admin" do
4 # setup do 4 # setup do
5 - # login_as_admin 5 + # sign_in_as Factory(:admin_user)
6 # get :index 6 # get :index
7 # end 7 # end
8 # should_paginate_collection :users 8 # should_paginate_collection :users
@@ -10,38 +10,38 @@ class Test::Unit::TestCase @@ -10,38 +10,38 @@ class Test::Unit::TestCase
10 # end 10 # end
11 def self.should_paginate_collection(collection_name) 11 def self.should_paginate_collection(collection_name)
12 should "paginate #{collection_name}" do 12 should "paginate #{collection_name}" do
13 - assert collection = assigns(collection_name), 13 + assert collection = assigns(collection_name),
14 "Controller isn't assigning to @#{collection_name.to_s}." 14 "Controller isn't assigning to @#{collection_name.to_s}."
15 - assert_kind_of WillPaginate::Collection, collection, 15 + assert_kind_of WillPaginate::Collection, collection,
16 "@#{collection_name.to_s} isn't a WillPaginate collection." 16 "@#{collection_name.to_s} isn't a WillPaginate collection."
17 end 17 end
18 end 18 end
19 - 19 +
20 def self.should_display_pagination 20 def self.should_display_pagination
21 should "display pagination" do 21 should "display pagination" do
22 - assert_select "div.pagination", { :minimum => 1 }, 22 + assert_select "div.pagination", { :minimum => 1 },
23 "View isn't displaying pagination. Add <%= will_paginate @collection %>." 23 "View isn't displaying pagination. Add <%= will_paginate @collection %>."
24 end 24 end
25 end 25 end
26 - 26 +
27 # Example: 27 # Example:
28 - # context "a GET to index not logged in as admin" do 28 + # context "a GET to index not signed in as admin" do
29 # setup { get :index } 29 # setup { get :index }
30 # should_not_paginate_collection :users 30 # should_not_paginate_collection :users
31 # should_not_display_pagination 31 # should_not_display_pagination
32 # end 32 # end
33 def self.should_not_paginate_collection(collection_name) 33 def self.should_not_paginate_collection(collection_name)
34 should "not paginate #{collection_name}" do 34 should "not paginate #{collection_name}" do
35 - assert collection = assigns(collection_name), 35 + assert collection = assigns(collection_name),
36 "Controller isn't assigning to @#{collection_name.to_s}." 36 "Controller isn't assigning to @#{collection_name.to_s}."
37 - assert_not_equal WillPaginate::Collection, collection.class, 37 + assert_not_equal WillPaginate::Collection, collection.class,
38 "@#{collection_name.to_s} is a WillPaginate collection." 38 "@#{collection_name.to_s} is a WillPaginate collection."
39 end 39 end
40 end 40 end
41 - 41 +
42 def self.should_not_display_pagination 42 def self.should_not_display_pagination
43 should "not display pagination" do 43 should "not display pagination" do
44 - assert_select "div.pagination", { :count => 0 }, 44 + assert_select "div.pagination", { :count => 0 },
45 "View is displaying pagination. Check your logic." 45 "View is displaying pagination. Check your logic."
46 end 46 end
47 end 47 end