title: RSpec controller specs: any way to ensure controller instance variables do not cross over between multiple 'gets'
body: <p>Is there any way in a controller spec to 'reset' the instance variable space before each get if we have to do multiple gets in the same spec context?</p>
  
  <p>I understand the one-assertion-per-test guideline. However, for one of our sets of tests, it runs about 3x faster if we don't do a separate (lengthy) before(:each) setup before each get and if we run a sequence of get/asserts together in a single context.</p>
  
  <p>However, it seems that (unlike when controller methods are invoked via browser) if you do two consecutive get's with rspec, instance variables are not cleared with each get, so data crossover is possible.</p>
  
  <p>Here's a failing test, showing that a variable set in 'vartest1' is still present when another controller method 'vartest2' is run:</p>
  
  <p>Controller methods:</p>
  
  <pre><code>  def vartest1
      @this_var = "foo" 
      render :text =&gt; @this_var
    end
  
    def vartest2
      render :text =&gt; @this_var # should be EMPTY!
    end
  </code></pre>
  
  <p>Rspec controller spec:</p>
  
  <pre><code>  describe "instance variable crossover example", :focus =&gt; true do
      describe "THIS PASSES put each get in separate contexts" do
        it "vartest1 outputs foo" do
          get "vartest1"
          response.body.should include("foo")
        end
        it "vartest2 does NOT output foo" do
          get "vartest2"
          response.body.should_not include("foo")
        end
      end
      describe "THIS FAILS put both gets in SAME context" do
        it "should not crossover controller instance varables" do
          get "vartest1"
          response.body.should include("foo")
          get "vartest2"
          response.body.should_not include("foo") # THIS FAILS
        end
      end
    end
  </code></pre>
  
  <p>Rspec results:</p>
  
  <pre><code> instance variable crossover example
      THIS PASSES put each get in separate contexts
        vartest1 outputs foo
        vartest2 does NOT output foo
      THIS FAILS put both gets in SAME context
        should not crossover controller instance varables (FAILED - 1)
  </code></pre>
  
  <p>What's happening in the failing test is that when rspec does <code>get 'vartest1'</code> the controller method set an instance variable to 'foo', and when rspec does <code>get 'vartest2'</code> the instance variable (which should be nil) is still set, so the test fails.</p>