title: Stubbing array responses with rspec
body: <p>I'm currently working on spec'ing out a rake task that's meant to populate a database using the <code>google_drive</code> gem. The gem allows users to access a spreadsheet <code>ws</code> like so:</p>
  
  <pre class="lang-rb prettyprint-override"><code># Gets content of A2 cell.
  p ws[2, 1]  #==&gt; "hoge"
  </code></pre>
  
  <p>The function that I'm trying to spec looks like this:</p>
  
  <pre class="lang-rb prettyprint-override"><code>def get_tags(ws, row)
    tags = []
    [1, 21, 22, 23, 26, 29, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40].each do |col|
      tags &lt;&lt; ws[row, col] unless ws[row, col].blank?
    end
    tags
  end
  </code></pre>
  
  <p>Fairly self explanatory, it gets passed a row and then adds the column data to <code>tags</code> for each column specified in the array.<br/></p>
  
  <p>My <code>ws</code> array consists of this:</p>
  
  <pre class="lang-rb prettyprint-override"><code>ws = [[nil, 'Good', 'Tultex', '0241', 'Men\s Blend Tee',
              'XS - 3XL', '$3.40', '$4.50', '$4.50', '--', '--', '--',
              'TSC', 'y', 'Adult Unisex', 'Tee Shirt', 'Short Sleeve',
              'Crewneck', '3.2', 'Cotton/Poly (35/36)', 'Fitted', '11',
              '0240 (ladies)', '', 'Std Fitted Crew', 'Tees and Tanks',
              'Nicer - Fashion Fitted', 'Blend', '', '', '', 'Fashionable',
              '', '', '']]
  </code></pre>
  
  <p>Therefore, I need <code>get_tags</code> to return this:</p>
  
  <pre class="lang-rb prettyprint-override"><code>resultant = ['Good', 'Tee Shirt', 'Short Sleeve', 'Crew Neck',
               'Standard', '', 'Tees and Tanks', 'Least Expensive',
               'Regular Cotton', '', '', '', '', '', '', 'Least Expensive']
  </code></pre>
  
  <p>My problem is that I have no idea how I can spec an array to accept the type of indexing that <code>ws</code> does (<code>ws[val, val]</code>), since that's usually a range index. I tried creating a two dimensional array but that obviously didn't work. Also tried stubbing it out by doing:</p>
  
  <pre class="lang-rb prettyprint-override"><code>allow(ws).to receive(:[]) do |arg1, arg2|
    ws[arg1][arg2]
  end
  </code></pre>
  
  <p>Which created an infinite loop of stubbiness, so then I tried storing <code>ws</code> into another array called <code>temp</code> and doing this (almost the same thing):</p>
  
  <pre class="lang-rb prettyprint-override"><code>allow(ws).to receive(:[]) do |arg1, arg2|
    temp[arg1][arg2]
  end
  </code></pre>
  
  <p>But I still end up with the same infinite stubbing loop.
  Any advice on what steps to pursue next would be greatly appreciated! Thanks in advance :)</p>