require 'teststrap' class TestModel < ActiveRecord::Base validates_presence_of :some_jank, :if => lambda { |test| test == "test" } validates_format_of :some_jank, :with => /\d/ validates_uniqueness_of :something_unique, :something_else_unique, :scope => :scope_id, :allow_nil => true validates_numericality_of :sanity_check validates_uniqueness_of :sanity_check validates_inclusion_of :sanity_check, :in => 0..1 validates_size_of :sanity_check, :in => 0..1 validates_confirmation_of :sanity_check validates_length_of :sanity_check, :in => 0..1 validates_format_of :sanity_check, :with => /\w/ validates_acceptance_of :sanity_check validates_exclusion_of :sanity_check, :in => 0..1 validates_presence_of :sanity_check end context "validation-mirror" do check_validation = lambda do |model, attr, macro_name| model.reflect_validations_on(attr).detect { |validation| validation.macro == macro_name && validation.attribute == attr } end context "reflecting on all validations" do should "return all the validations set" do TestModel.reflect_all_validations.size end.equals(14) end context "reflecting on validations for a single attribute" do setup { TestModel } should "capture all the standard AR validations we know of" do topic.reflect_validations_on(:sanity_check).size end.equals(10) context "asserting validation presence" do should "have a validates_presence_of on some_jank" do check_validation[TestModel, :some_jank, :validates_presence_of] end should "have a validates_format_of on some_jank" do check_validation[TestModel, :some_jank, :validates_format_of] end should "have a uniqueness validation on something_unique" do check_validation[TestModel, :something_unique, :validates_uniqueness_of] end should "have a uniqueness validation on something_else_unique" do check_validation[TestModel, :something_else_unique, :validates_uniqueness_of] end end # asserting validation presence context "asserting validation setup arguments" do should "have the correct arguments length for validates_presence_of on :some_jank" do validation = check_validation[TestModel, :some_jank, :validates_presence_of] validation.arguments.size end.equals(1) should "have stashed away the :if block on validates_presence_of for :some_jank" do validation = check_validation[TestModel, :some_jank, :validates_presence_of] validation.arguments[:if].call("test") end should "have the correct arguments length for validates_format_of on :some_jank" do validation = check_validation[TestModel, :some_jank, :validates_format_of] validation.arguments.size end.equals(1) should "have stashed away the arguments on validates_format_of for :some_jank" do validation = check_validation[TestModel, :some_jank, :validates_format_of] validation.arguments end.equals(:with => /\d/) should "have stashed away the arguments on validates_uniqueness_of for :something_unique" do validation = check_validation[TestModel, :something_unique, :validates_uniqueness_of] validation.arguments end.equals(:scope => :scope_id, :allow_nil => true) should "have stashed away the arguments on validates_uniqueness_of for :something_else_unique" do validation = check_validation[TestModel, :something_else_unique, :validates_uniqueness_of] validation.arguments end.equals(:scope => :scope_id, :allow_nil => true) end # asserting validation setup arguments end # reflecting on validations for a single attribute end