Created
May 4, 2011 16:28
-
-
Save AndrewO/955522 to your computer and use it in GitHub Desktop.
A formatter for Wrong that displays full error messages when an ActiveRecord instance is invalid
This file contains hidden or 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
# Can be placed in with your test helpers. Eventually I'm hoping to package as a gem | |
module Wrong | |
class ActiveRecordErrors < FailureMessage::Formatter | |
register | |
def match? | |
predicate.is_a?(Predicated::Call) && | |
object_is_an_active_record?(predicate.left) && | |
method_runs_validations?(predicate.method_sym) && | |
failed_because_of_validations?(predicate.left) | |
end | |
def describe | |
" the record is invalid:\n" + predicate.left.errors.full_messages.map {|fm| "\t* #{fm}"}.join("\n") | |
end | |
private | |
def object_is_an_active_record?(object) | |
object.is_a?(ActiveRecord::Base) | |
end | |
METHODS = [:valid?, :save!, :save, :create!, :create] | |
def method_runs_validations?(method) | |
METHODS.include?(method) | |
end | |
def failed_because_of_validations?(object) | |
!object.valid? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment