Method: Incline::Extensions::TestCase#assert_required
- Defined in:
- lib/incline/extensions/test_case.rb
#assert_required(model, attribute, message = nil, regex = /can't be blank/i) ⇒ Object
Tests a specific field for presence validation.
- model
-
This must respond to attribute and attribute= as well as valid? and errors.
- attribute
-
This must provide the name of a valid attribute in the model.
- message
-
This is optional, but if provided it will be postfixed with the failure reason.
- regex
-
This is the regex to match against the error message to ensure that the failure is for the correct reason.
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/incline/extensions/test_case.rb', line 264 def assert_required(model, attribute, = nil, regex = /can't be blank/i) original_value = model.send(attribute) assert model.valid?, 'Model should be valid to start.' is_string = original_value.is_a?(::String) setter = :"#{attribute}=" model.send setter, nil assert_not model.valid?, ? ( + ': (nil)') : "Should not allow #{attribute} to be set to nil." assert model.errors[attribute].to_s =~ regex, ? ( + ': (error message)') : 'Did not fail for expected reason.' if is_string model.send setter, '' assert_not model.valid?, ? ( + ": ('')") : "Should not allow #{attribute} to be set to empty string." assert model.errors[attribute].to_s =~ regex, ? ( + ': (error message)') : 'Did not fail for expected reason.' model.send setter, ' ' assert_not model.valid?, ? ( + ": (' ')") : "Should not allow #{attribute} to be set to blank string." assert model.errors[attribute].to_s =~ regex, ? ( + ': (error message)') : 'Did not fail for expected reason.' end model.send setter, original_value assert model.valid?, ? ( + ": !(#{original_value.inspect})") : "Should allow #{attribute} to be set back to '#{original_value.inspect}'." end |