# encoding: utf-8 require "spec_helper" require File.join(File.dirname(__FILE__), "..", "lib", "validates_falsity") describe ".validates_falsity_of" do it "allows false" do Truthteller.validates_falsity_of :i_am_not_a_crook truthteller = Truthteller.new(false) truthteller.should be_valid truthteller.errors[:i_am_not_a_crook].should be_empty end it "does not allow true" do Truthteller.validates_falsity_of :i_am_not_a_crook truthteller = Truthteller.new(true) truthteller.should_not be_valid truthteller.errors[:i_am_not_a_crook].should_not be_empty end it "works with Rails 3-style validation syntax" do Truthteller.validates :i_am_not_a_crook, :falsity => true Truthteller.validates :puppies_are_cute, :falsity => true truthteller = Truthteller.new(false) truthteller.i_am_not_a_crook = false truthteller.puppies_are_cute = "foo" truthteller.should_not be_valid truthteller.errors[:puppies_are_cute].should_not be_empty truthteller.errors[:i_am_not_a_crook].should be_empty end it "shows the English error message" do Truthteller.validates_falsity_of :i_am_not_a_crook truthteller = Truthteller.new("foo") truthteller.should_not be_valid truthteller.errors[:i_am_not_a_crook].should == ["must be false"] end it "shows the supplied error message if provided" do Truthteller.validates_falsity_of :i_am_not_a_crook, :message => "must answer 'yes'" truthteller = Truthteller.new("foo") truthteller.should_not be_valid truthteller.errors[:i_am_not_a_crook].should == ["must answer 'yes'"] end it "shows use I18n string as error message" do I18n.locale = :'pt-BR' Truthteller.validates_falsity_of :i_am_not_a_crook truthteller = Truthteller.new("foo") truthteller.should_not be_valid truthteller.errors[:i_am_not_a_crook].should == ["deve ser falsa"] end it "rejects nil value" do Truthteller.validates_falsity_of :i_am_not_a_crook truthteller = Truthteller.new(nil) truthteller.should_not be_valid truthteller.errors[:i_am_not_a_crook].should_not be_empty end it "does not reject nil value when asked not to" do Truthteller.validates_falsity_of :i_am_not_a_crook, :allow_nil => true truthteller = Truthteller.new(nil) truthteller.should be_valid truthteller.errors[:i_am_not_a_crook].should be_empty end it "aliases validates_truth_of" do Truthteller.should respond_to(:validates_falsehood_of) end end