public
Description: Some testing macros for the Shoulda plugin/gem.
Homepage: http://soyunperdedor.com/
Clone URL: git://github.com/mileszs/shoulda_macros.git
shoulda_macros / functionals.rb
100644 39 lines (34 sloc) 1.386 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Test::Unit::TestCase
 
  # should_have_form :action => 'admin_users_path',
  # :method => :get,
  # :fields => { 'email' => :text }
  # TODO: http_method should be pulled out (thoughtbot comment)
  # TODO: make it work with <select> elements (my comment)
  # See http://github.com/thoughtbot/clearance, which
  # is where I found this macro
  def self.should_have_form(opts)
    model = self.name.gsub(/ControllerTest$/, '').singularize.downcase
    model = model[model.rindex('::')+2..model.size] if model.include?('::')
    http_method, hidden_http_method = form_http_method opts[:method]
    should "have a #{model} form" do
      assert_select "form[action=?][method=#{http_method}]", eval(opts[:action]) do
        if hidden_http_method
          assert_select "input[type=hidden][name=_method][value=#{hidden_http_method}]"
        end
        opts[:fields].each do |attribute, type|
          attribute = attribute.is_a?(Symbol) ? "#{model}[#{attribute.to_s}]" : attribute
          assert_select "input[type=#{type.to_s}][name=?]", attribute
        end
        assert_select "input[type=submit]"
      end
    end
  end
  
  def self.form_http_method(http_method)
    http_method = http_method.nil? ? 'post' : http_method.to_s
    if http_method == "post" || http_method == "get"
      return http_method, nil
    else
      return "post", http_method
    end
  end
 
end