require 'spec_helper' describe InfoparkComponentCache::Guards::ValidFrom do let(:cache_component_stub) do double.tap do |cache_component_stub| cache_component_stub.stub(:cache_key) do |input| input.inspect end end end let(:minimum_valid_from) { 10.minutes.since.to_iso } let(:obj_stub) do double.tap do |obj_stub| obj_stub.stub(:scoped).and_return(obj_stub) obj_stub.stub(:where).and_return(obj_stub) obj_stub.stub(:minimum) do |field| minimum_valid_from if field == :valid_from end end end before { InfoparkComponentCache::CmsStateGuard.obj_root_class = obj_stub } subject { described_class.new(cache_component_stub) } context "with cache disabled" do disable_cache context "without a call to guard!" do it { is_expected.not_to be_consistent } end context "with a call to guard!" do before { subject.guard! } it { is_expected.not_to be_consistent } end end context "with cache enabled" do enable_cache before { Rails.cache.clear } context "without a call to guard!" do it { is_expected.not_to be_consistent } end context "with a call to guard!" do before { subject.guard! } it { is_expected.to be_consistent } end context "after minimum valid from changes to a present date" do before { subject.guard! } before { obj_stub.stub(:minimum).and_return(Time.now.to_iso) } it { is_expected.not_to be_consistent } end context "after minimum valid from changes to a past date" do before { subject.guard! } before { obj_stub.stub(:minimum).and_return(1.month.ago.to_iso) } it { is_expected.not_to be_consistent } end end end