require 'spec_helper' describe DateValidator do # Undo validations after each test before { Test.reset_callbacks(:validate) } context "with a :before key given" do before { Test.validates :date, date: { before: 2.days.from_now }} let!(:test) { Test.new } context "if an attribute's value is before given date" do before { test.date = 1.days.from_now.to_date } it "validation passes" do test.should be_valid test.errors.should be_blank end end context "if an attribute's value equals given date" do before { test.date = 2.days.from_now.to_date } it "validation fails" do test.should_not be_valid test.errors.should_not be_blank end end context "if an attribute's value is after given date" do before { test.date = 3.days.from_now.to_date } it "validation fails" do test.should_not be_valid test.errors.should_not be_blank end end context "if an attribute's value isn't set" do it "validation fails" do test.should_not be_valid test.errors.should_not be_blank end end end context "with an :after key given" do before { Test.validates :date, date: { after: 2.days.from_now }} let!(:test) { Test.new } context "if an attribute's value is before given date" do before { test.date = 1.days.from_now.to_date } it "validation fails" do test.should_not be_valid test.errors.should_not be_blank end end context "if an attribute's value equals given date" do before { test.date = 2.days.from_now.to_date } it "validation fails" do test.should_not be_valid test.errors.should_not be_blank end end context "if an attribute's value is after given date" do before { test.date = 3.days.from_now.to_date } it "validation passes" do test.should be_valid test.errors.should be_blank end end context "if an attribute's value isn't set" do it "validation fails" do test.should_not be_valid test.errors.should_not be_blank end end end context "with a :not_before key given" do before { Test.validates :date, date: { not_before: 2.days.from_now }} let!(:test) { Test.new } context "if an attribute's value is before given date" do before { test.date = 1.days.from_now.to_date } it "validation fails" do test.should_not be_valid test.errors.should_not be_blank end end context "if an attribute's value equals given date" do before { test.date = 2.days.from_now.to_date } it "validation passes" do test.should be_valid test.errors.should be_blank end end context "if an attribute's value is after given date" do before { test.date = 3.days.from_now.to_date } it "validation passes" do test.should be_valid test.errors.should be_blank end end context "if an attribute's value isn't set" do it "validation fails" do test.should_not be_valid test.errors.should_not be_blank end end end context "with a :not_after key given" do before { Test.validates :date, date: { not_after: 2.days.from_now }} let!(:test) { Test.new } context "if an attribute's value is before given date" do before { test.date = 1.days.from_now.to_date } it "validation passes" do test.should be_valid test.errors.should be_blank end end context "if an attribute's value equals given date" do before { test.date = 2.days.from_now.to_date } it "validation passes" do test.should be_valid test.errors.should be_blank end end context "if an attribute's value is after given date" do before { test.date = 3.days.from_now.to_date } it "validation fails" do test.should_not be_valid test.errors.should_not be_blank end end context "if an attribute's value isn't set" do it "validation fails" do test.should_not be_valid test.errors.should_not be_blank end end end context "with an :allow_nil option set to true" do before { Test.validates :date, date: { not_after: 2.days.from_now, allow_nil: true }} let!(:test) { Test.new } it "validation passes even if an attribute's value isn't set" do test.should be_valid test.errors.should be_blank end end end