The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
MockOpenURI

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Florian Weber

Posts: 94
Nickname: csshsh
Registered: Feb, 2005

Florian Weber is freelance developer from Hamburg, Germany
MockOpenURI Posted: Dec 5, 2005 10:02 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Florian Weber.
Original Post: MockOpenURI
Feed Title: Oakland Cutlery
Feed URL: http://blog.oaklandcutlery.com/wp-rss2.php
Feed Description: Ruby, Rails, Interface Design
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Florian Weber
Latest Posts From Oakland Cutlery

Advertisement

It’s a bad idea to have your tests depending on external resources (rss feeds, images, etc) on a http/ftp server. What happens if for some reason the server those files are on is offline?

You could either hide the access of the files behind a service and use a mock service in your tests or overwrite the method of your object that would normally access a file (for example Channel#read_rss_feed). Under some circumstances both solutions make things just more complicated and can cause all kinds of trouble.

Assuming you use open-uri’s open method to access those files, MockOpenURI lets you mock that out:

MockOpenURI.register('test_example.txt', :file => File.dirname(__FILE__) + '/test_example.txt')


open('http://mock/test_example.txt') acts like it would download test_example.txt from an online server now.

Using a mock domain different than the default ‘mock’:
MockOpenURI.register('http://www.foo.com/test_example.txt', :file => File.dirname(__FILE__) + '/test_example.txt')

Using a simple string:
MockOpenURI.register('test_string.txt', :string => 'foo')

Returning a specific status:
MockOpenURI.register('test_string.txt', :status => ['301', 'Moved Permanently'])

Raising exceptions for certiain URIs:
MockOpenURI.register('raising_exception.txt', :exception => SomeException.new('foo')) MockOpenURI.register('raising_exception.txt', :exception => OpenURI::HTTPError)

(Known) Issues

I’m sure there are quite a couple. Right now it’s a ‘good enough’-solution for us at Odeo. If you come across anything you think is missing, doesn’t work properly, etc.. please let me know.

Installation
gem install --source http//dist.oaklandcutlery.com mock_open_uri

Read: MockOpenURI

Topic: Try Ruby Previous Topic   Next Topic Topic: Renaming and picking methods from included modules---tastes like Eiffel?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use