Skip to content
This repository was archived by the owner on Jan 8, 2020. It is now read-only.

Commit 39aa025

Browse files
committed
add a custom http mock method, quite simple but efficient
1 parent b85c6eb commit 39aa025

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
*.gem
2+
spec/fixtures/*
3+
!spec/fixtures/.gitkeep

spec/fixtures/.gitkeep

Whitespace-only changes.

spec/mock_http.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
require 'rspec'
12
require_relative '../lib/suby'
3+
require_relative 'mock_http'

0 commit comments

Comments
 (0)