[rspec-users] Stubbing calls to the command line

Zach Dennis zach.dennis at gmail.com
Thu Aug 14 17:09:56 EDT 2008

On Thu, Aug 14, 2008 at 3:42 AM, Andy Croll <lists at ruby-forum.com> wrote:
> I'm using a call to wget to download a large datafile (multiple
> megabytes) having assumed this will be quicker than using open-uri.
> How can I spec the behaviour? Ideally I'd also like to not be hitting
> the internet every time I test!
> in my_object_spec.rb
>  describe ".get_data" do
>    before(:each) do
>      @my_obj = MyObject.create(@valid_attributes)
>    end
>    it "should download the file" do
>      # behaviour goes here
>    end
>  end
> in my_object.rb:
>  def get_data
>      `wget --output-document=#{self.filename} #{self.file_uri}`
>  end
> I'll be massively grateful for any help anyone can give me.

I might end up with a separate object which managed making the wget
system call, and then I've have an integration-style test which
ensured it correctly downloaded a given passed in URL.

class MyObject
   def get_data

class WGet
   def download(url)
      `wget #{url}`

And I probably wouldn't have a spec for WGet. I might make the
integration-style test a story. Perhaps when it ran I'd start
script/server in test mode, and then copy over a dummy file and pull
it down from "http://localhost:3000/public/dummy".

Zach Dennis

More information about the rspec-users mailing list