spec/unit/util/fact_spec.rb in facter-1.6.18 vs spec/unit/util/fact_spec.rb in facter-1.7.0.rc1

- old
+ new

@@ -30,43 +30,30 @@ describe "when adding resolution mechanisms" do before do @fact = Facter::Util::Fact.new("yay") - @resolution = mock 'resolution' - @resolution.stub_everything - + @resolution = Facter::Util::Resolution.new("yay") end - it "should fail if no block is given" do - lambda { @fact.add }.should raise_error(ArgumentError) - end - - it "should create a new resolution instance" do + it "should be to create a new resolution instance with a block" do Facter::Util::Resolution.expects(:new).returns @resolution @fact.add { } end - it "should instance_eval the passed block on the new resolution" do - @resolution.expects(:instance_eval) - - Facter::Util::Resolution.stubs(:new).returns @resolution - - @fact.add { } + @fact.add { + setcode { "foo" } + } + @fact.value.should == "foo" end it "should re-sort the resolutions by weight, so the most restricted resolutions are first" do - r1 = stub 'r1', :weight => 1 - r2 = stub 'r2', :weight => 2 - r3 = stub 'r3', :weight => 0 - Facter::Util::Resolution.expects(:new).times(3).returns(r1).returns(r2).returns(r3) - @fact.add { } - @fact.add { } - @fact.add { } - - @fact.instance_variable_get("@resolves").should == [r2, r1, r3] + @fact.add { self.value = "1"; self.weight = 1 } + @fact.add { self.value = "2"; self.weight = 2 } + @fact.add { self.value = "0"; self.weight = 0 } + @fact.value.should == "2" end end it "should be able to return a value" do Facter::Util::Fact.new("yay").should respond_to(:value) @@ -120,9 +107,34 @@ @fact.value.should be_nil end end - it "should have a method for flushing the cached fact" do - Facter::Util::Fact.new(:foo).should respond_to(:flush) + describe '#flush' do + subject do + Facter::Util::Fact.new(:foo) + end + context 'basic facts using setcode' do + it "flushes the cached value when invoked" do + system = mock('some_system_call') + system.expects(:data).twice.returns(100,200) + + subject.add { setcode { system.data } } + 5.times { subject.value.should == 100 } + subject.flush + subject.value.should == 200 + end + end + context 'facts using setcode and on_flush' do + it 'invokes the block passed to on_flush' do + model = { :data => "Hello World" } + subject.add do + on_flush { model[:data] = "FLUSHED!" } + setcode { model[:data] } + end + subject.value.should == "Hello World" + subject.flush + subject.value.should == "FLUSHED!" + end + end end end