title: Testing a Puppet custom function with RSpec, why can't I see facter variables?
body: <p>According to <a href="http://docs.puppetlabs.com/guides/custom_functions.html#using-facts-and-variables" rel="nofollow">http://docs.puppetlabs.com/guides/custom_functions.html#using-facts-and-variables</a> , I should be able to access facts by calling lookupvar</p>
  
  <pre><code>module Puppet::Parser::Functions
    newfunction(:machinelist2, :type =&gt; :rvalue) do
      lookupvar('operatingsystem')
    end
  end
  </code></pre>
  
  <p>Using this in spec, it returns nil.</p>
  
  <pre><code>context 'something' do
    let(:facts) {{:operatingsystem =&gt; 'blah'}}
    it "should return os" do
      result = scope.function_machinelist2([])
      result.should(eq('blah'))
    end
  end
  </code></pre>
  
  <p>(and running spec...)
  $ rspec</p>
  
  <pre><code>machinelist2 function
    should exist
    something
      should return os (FAILED - 1)
  
  Failures:
  
    1) machinelist2 function something should return os
       Failure/Error: result.should(eq('blah'))
  
         expected: "blah"
              got: nil
  
         (compared using ==)
       # ./spec/parser/functions/machinelist2_spec.rb:17
  
  Finished in 0.12478 seconds
  2 examples, 1 failure
  
  Failed examples:
  
  rspec ./spec/parser/functions/machinelist2_spec.rb:15 # machinelist2 function something should return os
  </code></pre>
  
  <p>Even if I remove the let(:facts) entry, I would expect the facter variable to resolve to the current operating system.</p>
  
  <p>It certainly does in my rspec-puppet tests for "regular" puppet code (class/module manifests)</p>
  
  <p><strong>UPDATE:</strong>
  I missed the part of the doc that says:</p>
  
  <blockquote>
    <p>To use a fact about a client, use lookupvar('FACT NAME') instead of
    Facter['FACT NAME'].value. If the fact does not exist, lookupvar
    returns:</p>
  </blockquote>
  
  <p>So, I tried the opposite, and used Facter['operatingsystem'].value .
  This will return my current operating system, but I can't override it using let().</p>
  
  <p>So, my question really is, HOW should I override facter variables to test custom functions that use them (facts)???</p>