Skip to content

Instantly share code, notes, and snippets.

@chrisk
Created May 7, 2010 02:57
Show Gist options
  • Save chrisk/392980 to your computer and use it in GitHub Desktop.
Save chrisk/392980 to your computer and use it in GitHub Desktop.
Using FakeWeb to stub out HTTP requests made by a server process from Cucumber's remote runners (Selenium, Mechanize, etc.)
# config/initializers/cucumber_fakeweb_hook.rb
# If this looks like a test server process, make its FakeWeb available to other
# processes via DRb. That way the test runner (which runs in a separate process)
# can dynamically register stubs for this server to use.
if Rails.env.cucumber? && defined?(PhusionPassenger)
require 'drb'
require 'fakeweb'
DRb.start_service("druby://localhost:30010", FakeWeb)
Rails.logger.info "=> Passenger's FakeWeb is available at #{DRb.uri}"
end
# features/support/env.rb
require 'drb'
require 'fakeweb'
# Make Passenger's FakeWeb available here in the test runner, so we can register
# stubs for the server to use
DRb.start_service
$server_fakeweb = DRbObject.new(nil, "druby://localhost:30010")
Before do
$server_fakeweb.clean_registry
$server_fakeweb.allow_net_connect = false
end
# features/step_definitions/fakeweb_steps.rb
Given "the example site responds with a 200 OK" do
$server_fakeweb.register_uri(:get, "http://example.com", :status => [200, "OK"])
end
Given "the example site responds with the following fixtures:" do |table|
response_path = Rails.root.join("features/support/fakeweb/example_site")
table.hashes.each do |stub|
http_method, uri = stub["HTTP method"].downcase.to_sym, stub["URI"]
filename_of_saved_response = response_path.join(stub["response"])
$server_fakeweb.register_uri(http_method, uri, :response => filename_of_saved_response)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment