Class: ActiveSupport::TestCase

Inherits:
Object
  • Object
show all
Defined in:
lib/has_helpers/test_helpers/shoulda_helpers.rb

Class Method Summary collapse

Class Method Details

.should_accept_nested_attributes_for(*attr_names) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
# File 'lib/has_helpers/test_helpers/shoulda_helpers.rb', line 236

def self.should_accept_nested_attributes_for(*attr_names)
  klass = self.name.gsub(/Test$/, "").constantize
  context "#{klass}" do
    attr_names.each do |association_name|
      should "accept nested attrs for #{association_name}" do
        assert klass.instance_methods.include?("#{association_name}_attributes=".to_sym),
               "#{klass} does not accept nested attributes for #{association_name}"
      end
    end
  end
end

.should_test_age_field(entity) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/has_helpers/test_helpers/shoulda_helpers.rb', line 107

def self.should_test_age_field(entity)
  should allow_value("100").for(entity)
  should allow_value(" 99").for(entity)
  should allow_value("1 ").for(entity)
  should allow_value("44").for(entity)
  should allow_value("0").for(entity)
  should allow_value("110").for(entity)
  should_not allow_value("111").for(entity).with_message(Validations.age_msg)
  should_not allow_value("-1").for(entity).with_message(Validations.age_msg)
  should_not allow_value("abc").for(entity).with_message(Validations.age_msg)
  should_not allow_value("&").for(entity).with_message(Validations.age_msg)
end

.should_test_dollar_field(entity) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/has_helpers/test_helpers/shoulda_helpers.rb', line 148

def self.should_test_dollar_field(entity)
  should allow_value("0").for(entity)
  should allow_value("1").for(entity)
  should allow_value("100").for(entity)
  should allow_value("1000").for(entity)
  should allow_value("1000.99").for(entity)
  should allow_value("1,000,00").for(entity)
  should allow_value("$1,000.00").for(entity)
  should allow_value("1,000,000").for(entity)
  should allow_value("1 000 000.01").for(entity)
  should allow_value("-0").for(entity)
  should allow_value("-1").for(entity)
  should allow_value("-100").for(entity)
  should allow_value("-1000").for(entity)
  should allow_value("-1000.99").for(entity)
  should allow_value("-1,000,00").for(entity)
  should allow_value("-$1,000.00").for(entity)
  should allow_value("-1,000,000").for(entity)
  should allow_value("-1 000 000.01").for(entity)
  should_not allow_value("0.2222").for(entity).with_message(Validations.dollar_msg)
  should_not allow_value("ewrt").for(entity).with_message(Validations.dollar_msg)
end

.should_test_location_field(entity, locations) ⇒ Object



7
8
9
10
# File 'lib/has_helpers/test_helpers/shoulda_helpers.rb', line 7

def self.should_test_location_field(entity, locations)
  should allow_value((locations + [nil, ""]).shuffle.first).for(entity)
  should_not allow_value(["test", "</", "abc123", "&nbsp;"].shuffle.first).for(entity)
end

.should_test_name_field(entity, length) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/has_helpers/test_helpers/shoulda_helpers.rb', line 12

def self.should_test_name_field(entity, length)
  should ensure_length_of(entity).is_at_least(0).is_at_most(length)
  if length > 8
    should allow_value("Kansas City").for(entity)
    should allow_value("-- Hey --").for(entity)
    should allow_value(" t o").for(entity)
    should allow_value("& you").for(entity)
    should allow_value("21 Jump").for(entity)
    should allow_value("Brookside").for(entity)
  end
  should allow_value("k c").for(entity)
  should allow_value("- H-").for(entity)
  should allow_value(" t").for(entity)
  should allow_value("& u").for(entity)
  should allow_value("21 ").for(entity)
  should allow_value("brok").for(entity)
  should_not allow_value(">*").for(entity).with_message(Validations.name_msg)
  should_not allow_value("< test").for(entity).with_message(Validations.name_msg)
end

.should_test_number_field(entity, length) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/has_helpers/test_helpers/shoulda_helpers.rb', line 171

def self.should_test_number_field(entity, length)
  if length > 8
    should allow_value("1,000,00").for(entity)
    should allow_value("1,000.00").for(entity)
    should allow_value("1,000,000").for(entity)
    should allow_value("1 000 000").for(entity)
  end
  should allow_value("0").for(entity)
  should allow_value("1").for(entity)
  should allow_value("100").for(entity)
  should allow_value("1000").for(entity)
  should_not allow_value("-1").for(entity).with_message(Validations.number_msg)
  should_not allow_value("werq").for(entity).with_message(Validations.number_msg)
end

.should_test_percent_field(entity) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/has_helpers/test_helpers/shoulda_helpers.rb', line 120

def self.should_test_percent_field(entity)
  should allow_value("100").for(entity)
  should allow_value(" 99").for(entity)
  should allow_value("1 ").for(entity)
  should allow_value("44").for(entity)
  should allow_value("0").for(entity)
  should allow_value("110").for(entity)
  should_not allow_value("161").for(entity).with_message(Validations.positive_percent_msg)
  should_not allow_value("-1").for(entity).with_message(Validations.positive_percent_msg)
  should_not allow_value("abc").for(entity).with_message(Validations.positive_percent_msg)
  should_not allow_value("&").for(entity).with_message(Validations.positive_percent_msg)
end

.should_test_phone_field(entity) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/has_helpers/test_helpers/shoulda_helpers.rb', line 46

def self.should_test_phone_field(entity)
  should allow_value("9134456677").for(entity)
  should allow_value("913 345 6677").for(entity)
  should allow_value(" 2134456677 ").for(entity)
  should allow_value("613-445-6677").for(entity)
  should allow_value("983445-6677").for(entity)
  should allow_value("(888)8888888").for(entity)
  should allow_value(" 913-4256677").for(entity)
  should allow_value("903-445 6627").for(entity)
  should allow_value("9134466677 ").for(entity)
  should allow_value("913-4556677").for(entity)
  should allow_value("513.445-6677").for(entity)
  should allow_value("555.555.8888").for(entity)
  should_not allow_value(">*").for(entity).with_message(Validations.phone_msg)
  should_not allow_value("< test").for(entity).with_message(Validations.phone_msg)
  should_not allow_value("www.test..com").for(entity).with_message(Validations.phone_msg)
  should_not allow_value("www.test.c").for(entity).with_message(Validations.phone_msg)
  should_not allow_value("www-test.com").for(entity).with_message(Validations.phone_msg)
  should_not allow_value("abc").for(entity).with_message(Validations.phone_msg)
  should_not allow_value("123").for(entity).with_message(Validations.phone_msg)
  should_not allow_value("&*()").for(entity).with_message(Validations.phone_msg)
  should_not allow_value("www.test-com").for(entity).with_message(Validations.phone_msg)
end

.should_test_positive_dollar_field(entity) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/has_helpers/test_helpers/shoulda_helpers.rb', line 133

def self.should_test_positive_dollar_field(entity)
  should allow_value("0").for(entity)
  should allow_value("1").for(entity)
  should allow_value("100").for(entity)
  should allow_value("1000").for(entity)
  should allow_value("1000.99").for(entity)
  should allow_value("1,000,00").for(entity)
  should allow_value("$1,000.00").for(entity)
  should allow_value("1,000,000").for(entity)
  should allow_value("1 000 000.01").for(entity)
  should_not allow_value("-1").for(entity).with_message(Validations.positive_dollar_msg)
  should_not allow_value("0.2222").for(entity).with_message(Validations.positive_dollar_msg)
  should_not allow_value("ewrt").for(entity).with_message(Validations.positive_dollar_msg)
end

.should_test_positive_percent_field(entity) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/has_helpers/test_helpers/shoulda_helpers.rb', line 91

def self.should_test_positive_percent_field(entity)
  should allow_value("100").for(entity)
  should allow_value("99").for(entity)
  should allow_value("1").for(entity)
  should allow_value("44").for(entity)
  should allow_value("99.999").for(entity)
  should allow_value("0.001").for(entity)
  should_not allow_value("101").for(entity).with_message(Validations.positive_percent_msg)
  should_not allow_value("0.2222").for(entity).with_message(Validations.positive_percent_msg)
  should_not allow_value("abc").for(entity).with_message(Validations.positive_percent_msg)
  should_not allow_value("&").for(entity).with_message(Validations.positive_percent_msg)
  should_not allow_value("-44").for(entity).with_message(Validations.positive_percent_msg)
  should_not allow_value("-44.4").for(entity).with_message(Validations.positive_percent_msg)
  should_not allow_value("-44.4444").for(entity).with_message(Validations.positive_percent_msg)
end

.should_test_rails_name_field(entity, length) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/has_helpers/test_helpers/shoulda_helpers.rb', line 186

def self.should_test_rails_name_field(entity, length)
  should assign_to(entity)
  should ensure_length_of(entity).is_at_least(0).is_at_most(length)
  should allow_value("kc_s").for(entity)
  should allow_value("hey").for(entity)
  should allow_value("yo_").for(entity)
  should allow_value("_jmp").for(entity)
  if length > 8
    should allow_value("kc_star_what").for(entity)
    should allow_value("hey").for(entity)
    should allow_value("yo_sucka").for(entity)
    should allow_value("_jump_street").for(entity)
  end
  should_not allow_value(">*").for(entity).with_message(Validations.rails_name_msg)
  should_not allow_value("< test").for(entity).with_message(Validations.rails_name_msg)
  should_not allow_value("test-er").for(entity).with_message(Validations.rails_name_msg)
  should_not allow_value("yo dude").for(entity).with_message(Validations.rails_name_msg)
end

.should_test_ssn_field(entity) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/has_helpers/test_helpers/shoulda_helpers.rb', line 220

def self.should_test_ssn_field(entity)
  should allow_value("515-99-8888").for(entity)
  should allow_value("515998488").for(entity)
  should allow_value(" ").for(entity)
  should allow_value(" 514998888 ").for(entity)
  should allow_value("544.99 8888").for(entity)
  should allow_value("515 99 8858").for(entity)
  should allow_value("444.33.6666").for(entity)
  should_not allow_value("ab-444fgh").for(entity).with_message(Validations.ssn_msg)
  should_not allow_value("abc").for(entity).with_message(Validations.ssn_msg)
  should_not allow_value("56599858>").for(entity).with_message(Validations.ssn_msg)
  should_not allow_value("33445<").for(entity).with_message(Validations.ssn_msg)
  should_not allow_value("3456356&").for(entity).with_message(Validations.ssn_msg)
  should_not allow_value("/23452").for(entity).with_message(Validations.ssn_msg)
end

.should_test_taxid_field(entity) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/has_helpers/test_helpers/shoulda_helpers.rb', line 205

def self.should_test_taxid_field(entity)
  should allow_value("51-5998888").for(entity)
  should allow_value("010000000").for(entity)
  should allow_value(" ").for(entity)
  should allow_value(" 514998888 ").for(entity)
  should allow_value("545998888").for(entity)
  should allow_value("51 5998858").for(entity)
  should allow_value("44.5559999").for(entity)
  should_not allow_value("ab-cdefgh").for(entity).with_message(Validations.taxid_msg)
  should_not allow_value("001000000").for(entity).with_message(Validations.taxid_msg)
  should_not allow_value("abc").for(entity).with_message(Validations.taxid_msg)
  should_not allow_value("<").for(entity).with_message(Validations.taxid_msg)
  should_not allow_value("&").for(entity).with_message(Validations.taxid_msg)
end

.should_test_url_field(entity) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/has_helpers/test_helpers/shoulda_helpers.rb', line 70

def self.should_test_url_field(entity)
  should allow_value("http://www.example.com").for(entity)
  should allow_value("www.example.com:8001").for(entity)
  should allow_value("http://www.exmple.com/1/abc?test=test").for(entity)
  should allow_value("http://finane.example.com").for(entity)
  should allow_value("http://www.example.com/1/abc?test=test").for(entity)
  should allow_value("www.example.com").for(entity)
  should allow_value("http://fiance.example.com").for(entity)
  should allow_value("finance.example.com").for(entity)
  should allow_value("http://finance.example.com.ag").for(entity)
  should_not allow_value(">*").for(entity).with_message(Validations.url_msg)
  should_not allow_value("< test").for(entity).with_message(Validations.url_msg)
  should_not allow_value("www.test..com").for(entity).with_message(Validations.url_msg)
  should_not allow_value("www.test.c").for(entity).with_message(Validations.url_msg)
  should_not allow_value("www-test.com").for(entity).with_message(Validations.url_msg)
  should_not allow_value("abc").for(entity).with_message(Validations.url_msg)
  should_not allow_value("123").for(entity).with_message(Validations.url_msg)
  should_not allow_value("&*()").for(entity).with_message(Validations.url_msg)
  should_not allow_value("www.test-com").for(entity).with_message(Validations.url_msg)
end

.should_test_zipcode_field(entity) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/has_helpers/test_helpers/shoulda_helpers.rb', line 32

def self.should_test_zipcode_field(entity)
  should allow_value("k c").for(entity)
  should allow_value("55555").for(entity)
  should allow_value("55555-5555").for(entity)
  should allow_value("55555 5555").for(entity)
  should allow_value("55555.5555").for(entity)
  should allow_value("(888)88-9999").for(entity)
  should allow_value(" ,-99999").for(entity)
  should_not allow_value("blah").for(entity).with_message(Validations.zipcode_msg)
  should_not allow_value("b lah").for(entity).with_message(Validations.zipcode_msg)
  should_not allow_value("3333").for(entity).with_message(Validations.zipcode_msg)
  should_not allow_value("333333").for(entity).with_message(Validations.zipcode_msg)
end