Commit 867da600ce2e197b1c4c1cd89e9899802fb073ee
1 parent
1198ed35
Exists in
master
and in
1 other branch
cleaning up shoulda macros
Showing
2 changed files
with
28 additions
and
47 deletions
Show diff stats
test/shoulda_macros/forms.rb
1 | 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 | 2 | # assert_form posts_url, :put do |
30 | 3 | # assert_text_field :post, :title |
31 | 4 | # assert_text_area :post, :body |
... | ... | @@ -56,26 +29,34 @@ class Test::Unit::TestCase |
56 | 29 | assert_select "input[type=submit]" |
57 | 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 | 36 | assert_select "input[type=text][name=?]", |
62 | 37 | "#{model.to_s}[#{attribute.to_s}]" |
63 | 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 | 44 | assert_select "textarea[name=?]", |
68 | 45 | "#{model.to_s}[#{attribute.to_s}]" |
69 | 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 | 52 | assert_select "input[type=password][name=?]", |
74 | 53 | "#{model.to_s}[#{attribute.to_s}]" |
75 | 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 | 60 | assert_select "input[type=radio][name=?]", |
80 | 61 | "#{model.to_s}[#{attribute.to_s}]" |
81 | 62 | end | ... | ... |
test/shoulda_macros/pagination.rb
1 | 1 | class Test::Unit::TestCase |
2 | 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 | 4 | # setup do |
5 | - # login_as_admin | |
5 | + # sign_in_as Factory(:admin_user) | |
6 | 6 | # get :index |
7 | 7 | # end |
8 | 8 | # should_paginate_collection :users |
... | ... | @@ -10,38 +10,38 @@ class Test::Unit::TestCase |
10 | 10 | # end |
11 | 11 | def self.should_paginate_collection(collection_name) |
12 | 12 | should "paginate #{collection_name}" do |
13 | - assert collection = assigns(collection_name), | |
13 | + assert collection = assigns(collection_name), | |
14 | 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 | 16 | "@#{collection_name.to_s} isn't a WillPaginate collection." |
17 | 17 | end |
18 | 18 | end |
19 | - | |
19 | + | |
20 | 20 | def self.should_display_pagination |
21 | 21 | should "display pagination" do |
22 | - assert_select "div.pagination", { :minimum => 1 }, | |
22 | + assert_select "div.pagination", { :minimum => 1 }, | |
23 | 23 | "View isn't displaying pagination. Add <%= will_paginate @collection %>." |
24 | 24 | end |
25 | 25 | end |
26 | - | |
26 | + | |
27 | 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 | 29 | # setup { get :index } |
30 | 30 | # should_not_paginate_collection :users |
31 | 31 | # should_not_display_pagination |
32 | 32 | # end |
33 | 33 | def self.should_not_paginate_collection(collection_name) |
34 | 34 | should "not paginate #{collection_name}" do |
35 | - assert collection = assigns(collection_name), | |
35 | + assert collection = assigns(collection_name), | |
36 | 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 | 38 | "@#{collection_name.to_s} is a WillPaginate collection." |
39 | 39 | end |
40 | 40 | end |
41 | - | |
41 | + | |
42 | 42 | def self.should_not_display_pagination |
43 | 43 | should "not display pagination" do |
44 | - assert_select "div.pagination", { :count => 0 }, | |
44 | + assert_select "div.pagination", { :count => 0 }, | |
45 | 45 | "View is displaying pagination. Check your logic." |
46 | 46 | end |
47 | 47 | end | ... | ... |