Created
June 22, 2011 15:10
-
-
Save ericcf/1040296 to your computer and use it in GitHub Desktop.
FakeWeb setup for testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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