Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add a custom http mock method, quite simple but efficient

  • Loading branch information...
commit 39aa0255a2f64d556d6ffe0e4360d18730c10dec 1 parent b85c6eb
Benoit Daloze authored
2  .gitignore
... ...
@@ -1 +1,3 @@
1 1
 *.gem
  2
+spec/fixtures/*
  3
+!spec/fixtures/.gitkeep
0  spec/fixtures/.gitkeep
No changes.
22  spec/mock_http.rb
... ...
@@ -0,0 +1,22 @@
  1
+class Suby::Downloader
  2
+  caches = {}
  3
+  [:get, :post, :get_redirection].each { |meth|
  4
+    original_method = instance_method(meth)
  5
+    remove_method meth
  6
+    define_method(meth) { |*args|
  7
+      file = 'spec/fixtures/' + self.class::SITE.downcase + '.marshal'
  8
+      caches[file] ||= File.exist?(file) ? Marshal.load(IO.read(file)) : {}
  9
+      data = caches[file]
  10
+
  11
+      if data[args]
  12
+        data[args]
  13
+      else
  14
+        puts "doing the real request: #{meth}(#{args * ', '})"
  15
+        value = original_method.bind(self).call(*args)
  16
+        data[args] = value
  17
+        File.write(file, Marshal.dump(data))
  18
+        value
  19
+      end
  20
+    }
  21
+  }
  22
+end
2  spec/spec_helper.rb
... ...
@@ -1 +1,3 @@
  1
+require 'rspec'
1 2
 require_relative '../lib/suby'
  3
+require_relative 'mock_http'

0 notes on commit 39aa025

Please sign in to comment.
Something went wrong with that request. Please try again.