This is a funcitonal test helper, which can be used to create methods on the fly to iterate through your controller methods and test for validations, url parameters and expected responses. It keeps the tests from getting as cluttered.
1
2
3
4
5
6
7
8 def test_required_attributes_in_controller( model, options, actions,*required_fields )
9
10 sym = model.to_s.downcase.to_sym
11 for field in required_fields
12 options[:params][sym][field] = ''
13 end
14
15 actions.each do |action|
16 self.class_eval do
17 define_method("test_#{action}_method_requires_fields") do
18 assert_nothing_raised do
19 post action, options[:params],options[:session]
20 end
21 for field in required_fields
22 assert !assigns(sym).errors[field].empty? unless options[:params][sym][field] == ''
23 end
24 assert !assigns.empty?,"This method requires specific parameters, yet no errors were created with them missing"
25 end
26 end
27 end
28 end
29
30
31
32
33
34
35
36
37 def test_required_unique_attributes_in_controller( model, options, actions,*required_fields )
38 sym = model.to_s.downcase.to_sym
39 for field in required_fields
40 options[:params][sym][field] = ''
41 end
42
43 actions.each do |action|
44 self.class_eval do
45 define_method("test_#{action}_method_requires_unique_fields") do
46 assert_nothing_raised do
47 post action, options[:params],options[:session]
48 end
49 for field in required_fields
50 assert !assigns(sym).errors[field].empty? unless options[:params][sym][field] == ''
51 end
52 assert !assigns.empty?,"This method fails without unique parameters, yet no errors were created"
53 end
54 end
55 end
56 end
57
58
59
60
61
62
63
64 def test_required_url_parameters_in_controller(model, options, *actions)
65 sym = model.to_s.downcase.to_sym
66 actions.each do |action|
67 self.class_eval do
68 define_method("test_#{action}_method_requires_url_parameters") do
69 post action, options[:params],options[:session]
70 assert_response :redirect
71 assert_nil assigns(sym),"This method requires a url parameter, yet the controller still created an object."
72 end
73 end
74 end
75 end
76
77 end