require 'spec_helper' describe TimeValidator do # Undo validations after each test before { Test.reset_callbacks(:validate) } let!(:time) { Time.now } context "with a :before key given" do before { Test.validates :time, time: { before: time.since(2) }} let!(:test) { Test.new } context "if an attribute's value is before given time" do before { test.time = time.since(1) } it "validation passes" do test.should be_valid test.errors.should be_blank end end context "if an attribute's value equals given time" do before { test.time = time.since(2) } 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 time" do before { test.time = time.since(3) } 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 :time, time: { after: time.since(2) }} let!(:test) { Test.new } context "if an attribute's value is before given time" do before { test.time = time.since(1) } 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 time" do before { test.time = time.since(2) } 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 time" do before { test.time = time.since(3) } 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 :time, time: { not_before: time.since(2) }} let!(:test) { Test.new } context "if an attribute's value is before given time" do before { test.time = time.since(1) } 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 time" do before { test.time = time.since(2) } it "validation passes" do test.should be_valid test.errors.should be_blank end end context "if an attribute's value is after given time" do before { test.time = time.since(3) } 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 :time, time: { not_after: time.since(2) }} let!(:test) { Test.new } context "if an attribute's value is before given time" do before { test.time = time.since(1) } it "validation passes" do test.should be_valid test.errors.should be_blank end end context "if an attribute's value equals given time" do before { test.time = time.since(2) } it "validation passes" do test.should be_valid test.errors.should be_blank end end context "if an attribute's value is after given time" do before { test.time = time.since(3) } 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 :time, time: { not_after: time.since(2), 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