Created
          July 9, 2010 17:03 
        
      - 
      
- 
        Save robotarmy/469715 to your computer and use it in GitHub Desktop. 
    Cache Fake Web Content to Disk and Register a URI
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # Example | |
| # fan = Factory(:fan) | |
| # FakeWeb.register(:get,"http://www.google.com/",fan.class) | |
| # fan.visit_google #successive calls read cache | |
| # | |
| # recommended to put into spec/support | |
| # | |
| require 'fakeweb' | |
| require 'open-uri' | |
| require 'fileutils' | |
| module FakeWeb | |
| class << self | |
| def register(method,uri,klass) | |
| file_path = File.join(File.dirname(__FILE__),to_s,"#{klass.to_s}.#{uri.hash}.fakeweb") | |
| content = nil | |
| unless File.exists?(file_path) | |
| FileUtils.mkdir_p(File.dirname(file_path)) | |
| content = open(uri).read | |
| File.open(file_path,File::CREAT|File::TRUNC|File::RDWR) do |f| | |
| f << content | |
| end | |
| else | |
| content = File.open(file_path).readlines.join | |
| end | |
| self.register_uri(method,uri,:body => content) | |
| end | |
| end | |
| end | 
Is better.
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
If you want something that helps you record and replay HTTP requests, check out VCR.