# encoding: utf-8 require "spec_helper" require File.join(File.dirname(__FILE__), "..", "lib", "validates_truthiness") describe ".validates_truthiness_of" do it "allows true" do Truthteller.validates_truthiness_of :i_am_not_a_crook truthteller = Truthteller.new(true) truthteller.should be_valid truthteller.errors[:i_am_not_a_crook].should be_empty end it "allows false" do Truthteller.validates_truthiness_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 "works with Rails 3-style validation syntax" do Truthteller.validates :i_am_not_a_crook, :truthiness => true Truthteller.validates :puppies_are_cute, :truthiness => true truthteller = Truthteller.new(true) truthteller.puppies_are_cute = "foo" truthteller.i_am_not_a_crook = true truthteller.should_not be_valid truthteller.errors[:i_am_not_a_crook].should be_empty truthteller.errors[:puppies_are_cute].should_not be_empty end it "shows the English error message" do Truthteller.validates_truthiness_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 either true or false"] end it "shows the supplied error message if provided" do Truthteller.validates_truthiness_of :the_sky_is_blue, :message => "must be either yes or no" truthteller = Truthteller.new("foo") truthteller.should_not be_valid truthteller.errors[:the_sky_is_blue].should == ["must be either yes or no"] end it "shows use I18n string as error message" do I18n.locale = :'pt-BR' Truthteller.validates_truthiness_of :puppies_are_cute truthteller = Truthteller.new("foo") truthteller.should_not be_valid truthteller.errors[:puppies_are_cute].should == ["deve ser verdadeira ou falsa"] end it "rejects nil value" do Truthteller.validates_truthiness_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_truthiness_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_booleanship_of" do Truthteller.should respond_to(:validates_booleanship_of) end end