Skip to content

Instantly share code, notes, and snippets.

@ericcf
Created June 22, 2011 15:10
Show Gist options
  • Save ericcf/1040296 to your computer and use it in GitHub Desktop.
Save ericcf/1040296 to your computer and use it in GitHub Desktop.
FakeWeb setup for testing
require 'fakeweb'
# this can also be set to a regular expression to whitelist URI's
FakeWeb.allow_net_connect = false
def load_fixture(name)
path = "#{Rails.root}/features/fixtures/#{name}.rb"
eval File.open(path).map { |line| line }.join("")
end
ingredients = load_fixture "ingredients"
FakeWeb.register_uri :get
"#{MyApplication::Application.config.ingredient_api_url}/ingredients.json",
:body => ingredients.to_json,
:status => ["304", "Not Modified"], # simulates a cached response
:content_type => "application/json"
ingredients.each do |ingredient|
FakeWeb.register_uri :get,
"#{MyApplication::Application.config.ingredient_api_url}/ingredients/#{ingredient['id']}.json",
:body => ingredient.to_json,
:status => ["304", "Not Modified"],
:content_type => "application/json"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment