Permalink
Browse files

add a custom http mock method, quite simple but efficient

  • Loading branch information...
eregon committed Jul 28, 2011
1 parent b85c6eb commit 39aa0255a2f64d556d6ffe0e4360d18730c10dec
Showing with 26 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. 0 spec/fixtures/.gitkeep
  3. +22 −0 spec/mock_http.rb
  4. +2 −0 spec/spec_helper.rb
View
@@ -1 +1,3 @@
*.gem
+spec/fixtures/*
+!spec/fixtures/.gitkeep
View
No changes.
View
@@ -0,0 +1,22 @@
+class Suby::Downloader
+ caches = {}
+ [:get, :post, :get_redirection].each { |meth|
+ original_method = instance_method(meth)
+ remove_method meth
+ define_method(meth) { |*args|
+ file = 'spec/fixtures/' + self.class::SITE.downcase + '.marshal'
+ caches[file] ||= File.exist?(file) ? Marshal.load(IO.read(file)) : {}
+ data = caches[file]
+
+ if data[args]
+ data[args]
+ else
+ puts "doing the real request: #{meth}(#{args * ', '})"
+ value = original_method.bind(self).call(*args)
+ data[args] = value
+ File.write(file, Marshal.dump(data))
+ value
+ end
+ }
+ }
+end
View
@@ -1 +1,3 @@
+require 'rspec'
require_relative '../lib/suby'
+require_relative 'mock_http'

0 comments on commit 39aa025

Please sign in to comment.