Created
July 9, 2009 07:29
-
-
Save mdeering/143497 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module CustomMatchers | |
class HaveActiveRecordTimestamps | |
def description | |
"have the active record timestamp columns created_at and updated_at" | |
end | |
def initialize() | |
end | |
def failure_message | |
"#{@object.class_name} is missing the active record timestamp columns created_at and updated_at" | |
end | |
def matches?(object) | |
@object = object | |
@object.respond_to?(:created_at) && @object.respond_to?(:updated_at) | |
end | |
def negative_failure_message | |
"#{@object.class_name} is has the active record timestamp columns created_at and updated_at" | |
end | |
end | |
def have_active_record_timestamps | |
HaveActiveRecordTimestamps.new | |
end | |
class ValidateLengthOf | |
def description | |
"only allow a #{@restriction} of #{@value} characters for #{@attribute}" | |
end | |
def initialize(attribute, restriction, value) | |
@attribute = attribute | |
@restriction = restriction | |
@value = value | |
end | |
def failure_message | |
"The restriction of a #{@restriction} of #{@value} characters is not being correctly enforced on #{@attribute}" | |
end | |
def matches?(object) | |
@object = object | |
@object.send("#{@attribute}=", '*' * @value) | |
@object.valid? | |
edge_value_excepted = @object.errors_on(@attribute).blank? | |
bumpped_value = case @restriction | |
when :minimum then @value -1 | |
else @value + 1 | |
end | |
@object.send("#{@attribute}=", '*' * bumpped_value) | |
@object.valid? | |
[email protected]_on(@attribute).blank? && edge_value_excepted | |
end | |
def negative_failure_message | |
"It does not really make sense to negate this test! ValidatesLengthOf" | |
end | |
end | |
def validate_length_of(attribute, restriction, value) | |
ValidateLengthOf.new(attribute, restriction, value) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment