Class: Sanitize::Rails::Matchers::SanitizeFieldsMatcher
- Inherits:
-
Object
- Object
- Sanitize::Rails::Matchers::SanitizeFieldsMatcher
- Defined in:
- lib/sanitize/rails/matchers.rb
Overview
Actual matcher class
Instance Method Summary collapse
- #description ⇒ Object
- #failure_message_for_should ⇒ Object
- #failure_message_for_should_not ⇒ Object
-
#initialize(*fields) ⇒ SanitizeFieldsMatcher
constructor
Take an array of fields to check, they must respect the same order given in model ‘sanitize` call.
-
#matches?(instance) ⇒ Boolean
Actual match code.
-
#replacing(invalid) ⇒ Object
Used to specify invalid text assigned to fields.
-
#with(valid) ⇒ Object
Used to specify expected output for the invalid text.
Constructor Details
#initialize(*fields) ⇒ SanitizeFieldsMatcher
Take an array of fields to check, they must respect the same order given in model ‘sanitize` call
37 38 39 40 41 |
# File 'lib/sanitize/rails/matchers.rb', line 37 def initialize(*fields) self. = fields. self.sanitizer = ::Sanitize::Rails::Engine.method_for(fields) self.fields = fields end |
Instance Method Details
#description ⇒ Object
76 77 78 |
# File 'lib/sanitize/rails/matchers.rb', line 76 def description "sanitize #{should_helper}" end |
#failure_message_for_should ⇒ Object
68 69 70 |
# File 'lib/sanitize/rails/matchers.rb', line 68 def "Expected #{should_helper} to return sanitized value '#{valid_value}', got '#{attribute_values}'" end |
#failure_message_for_should_not ⇒ Object
72 73 74 |
# File 'lib/sanitize/rails/matchers.rb', line 72 def "Expected #{field_helper} not to be sanitized" end |
#matches?(instance) ⇒ Boolean
Actual match code
58 59 60 61 62 63 64 65 66 |
# File 'lib/sanitize/rails/matchers.rb', line 58 def matches?(instance) self.instance = instance # assign invalid value to each field fields.each { |field| instance.send("#{field}=", invalid_value) } # sanitize the object calling the method instance.send(sanitizer) rescue nil # check expectation on results fields.all? { |field| valid_value == instance.send(field) } end |
#replacing(invalid) ⇒ Object
Used to specify invalid text assigned to fields
44 45 46 47 48 |
# File 'lib/sanitize/rails/matchers.rb', line 44 def replacing(invalid) @invalid_changed = true @invalid = invalid self end |
#with(valid) ⇒ Object
Used to specify expected output for the invalid text
51 52 53 54 55 |
# File 'lib/sanitize/rails/matchers.rb', line 51 def with(valid) @valid_changed = true @valid = valid self end |