title: Rspec it{} evaluated uses last variable value
body: <p>I'm using Rspec to make some tests for my program. In a spec I instantiate the class once, and perform my tests on it using <em>describes</em> and <em>contexts</em>. I encountered something interesting, if the <em>its</em> seem to be evaluated at the end of the contexts. For example given the following classes and its associated spec:</p>
  
  <pre><code>class Tmp
    def initialize
      @values = {}
    end
  
    def modify(new_value1, new_value2)
      @values = {:a =&gt; new_value1, :b =&gt; new_value2}
    end
  
    def get_values
      return @values
    end
  end
  </code></pre>
  
  <hr>
  
  <pre><code>describe Tmp do
    tmp = Tmp.new()
  
    describe "testing something" do
      context "change value" do
  
        # First evaluation
        tmp.modify("hi", "bye")
        it {tmp.get_values.should == {:a =&gt; "hi", :b =&gt; "bye"}}
  
        # Second evaluation
        tmp.modify("bye", "hi")
        it {tmp.get_values.should == {:a =&gt; "bye", :b =&gt; "hi"}}
      end
    end
  end
  </code></pre>
  
  <hr>
  
  <p>Using the provided class and spec the results are as follows:</p>
  
  <pre><code>F.
  
  Failures:
  
    1) Tmp testing something change value 
       Failure/Error: it {tmp.get_values.should == {:a =&gt; "hi", :b =&gt; "bye"}}
         expected: {:a=&gt;"hi", :b=&gt;"bye"}
              got: {:a=&gt;"bye", :b=&gt;"hi"} (using ==)
         Diff:
         @@ -1,3 +1,3 @@
         -:a =&gt; "hi",
         -:b =&gt; "bye"
         +:a =&gt; "bye",
         +:b =&gt; "hi"
       # ./spec/tmp_spec.rb:11:in `block (4 levels) in &lt;top (required)&gt;'
  
  Finished in 0.00211 seconds
  2 examples, 1 failure
  </code></pre>
  
  <p>This is interesting as Rspec seems to evaluate the first <em>it</em> with the values from <em>tmp</em> as it is at the end of the context. Even though within the context the <em>tmp</em> is changing its values and <strong>should</strong> pass, Rspec evaluates the <em>its</em> based on the last values that variables have at the ending (I even tried this with a local primitive variable within the <em>context</em> and has a similar experience).</p>
  
  <p>Is there a way to get around this and have the <em>its</em> evaluated in order? Or to at least get the following test to pass? I know I can use different variables and it will work, but there must be a way around this. I also want to know if this is an intended effect for Rspec.</p>
  
  <p><strong>UPDATE with respect to Philipe's answer</strong></p>
  
  <p>By making the change within a single <em>it</em> block the spec passes:</p>
  
  <pre><code>describe Tmp do
    describe "do something" do
      let(:instance) {Tmp.new}
  
      it 'should be modifiable' do
        instance.modify('hi', 'bye')
        instance.values.should == {a: 'hi', b: 'bye'}
        instance.modify('bye', 'hi')
        instance.values.should == {a: 'bye', b: 'hi'}
      end
    end
  end
  </code></pre>
  
  <p>But if I use the <em>subject</em> it seems to revert back and fails on the first <em>should</em></p>
  
  <pre><code>describe Tmp do
    describe "do something" do
      let(:instance) {Tmp.new}
      subject{instance.values}
  
      it 'should be modifiable' do
        instance.modify('hi', 'bye')
        should == {a: 'hi', b: 'bye'}
        instance.modify('bye', 'hi')
        should == {a: 'bye', b: 'hi'}
      end
    end
  end
  </code></pre>
  
  <p>Not sure why this is the case. At least I see that the changes should be within a <em>it</em> block to better reflect the changes we're testing for.</p>