Generating fake responses by caching real ones once first.

30 views
Skip to first unread message

Ben Marini

unread,
Jun 21, 2010, 10:47:55 PM6/21/10
to FakeWeb
I have a third party API that I'd like to mock with FakeWeb, but I was
hoping FakeWeb could generate the mocked responses for me the first
time around. What I want would be something like this:

# Setup some type of caching for the request responses, for
example:
CACHE = ActiveSupport::Cache::FileStore.new "test/fixtures/api/"

# If passed a block, yields the method, normalized uri, and
# Net::Http request object
FakeWeb.register_uri(:all, /.*/) do |method, uri, request|
CACHE.fetch(uri) do
# Forces a real request through by temporarily allowing a net
request
FakeWeb.force(request)
end
end

This way I could easily generate the right mock responses for the
requests I make, and then manually tweak them in the "fixture" or
cache file.

Has anyone done something like this already? I'd like to implement
this but want a second opinion since I'm new to this gem.
Reply all
Reply to author
Forward
0 new messages