Stubbing out enumerable :each in Rspec

Ran into a tricky situation the other day trying mock out an enumerable object. I was trying to test a list of a files inside a Zip. However, I couldn't figure out how to yield a list of mocked ZipFile objects without monkey patching something inside Rubyzip which didn't seem very attractive at the time.... Instead I decided to simply stub! the :each method and create a helper

  def mock_out_enumerable_each(*files)  
    block = lambda {|block| files.each{|n|}}

This works rather nicely, 

one = mock('one', :name => "assets/file1.jpg")
two = mock('two', :name => "assets/file2.png")
three = mock('three', :name => "assets/file3.gif")
four = mock('four', :name => "assets/file4.gif")
stub_out_enumerable_each one,two,three,four  

Go Back