lib/fake_mechanize/agent.rb in fake_mechanize-0.0.0 vs lib/fake_mechanize/agent.rb in fake_mechanize-0.0.1

- old
+ new

@@ -1,6 +1,12 @@ +# = FakeMechanize module +# FakeMechanize provides methods and classes to write tests for applications which relies on Mechanize. +# == Examples +# module FakeMechanize + # Agent acts like the original Mechanize::Agent but totally offline. + # It provides a respond_to method to predefine queries and their answers. class Agent attr_accessor :cookie_jar def initialize @cookie_jar = WWW::Mechanize::CookieJar.new @@ -13,18 +19,28 @@ reset_responses! yield Responder.new(@responses, @errors) @errors << ErrorRequest.new(:status => 404, :body => "not found") end - def get(uri) - return_mechanize_response Request.new(:uri => uri) + def assert_queried(method, uri, params = {}) + request = Request.new(:method => method, :uri => uri, :request_headers => params) + @history.any? {|history_query| history_query == request} end - def post(uri, args = {}) - return_mechanize_response Request.new(:method => :post, :uri => uri, :request_headers => args) + HttpVerbs.each do |method| + module_eval <<-EOE, __FILE__, __LINE__ + def was_#{method}?(uri, params = {}) + assert_queried(:#{method}, uri, params) + end + + def #{method}(uri, args = {}) + return_mechanize_response Request.new(:method => :#{method}, :uri => uri, :request_headers => args) + end + EOE end + protected def return_mechanize_response(given_request) @history << given_request request = search_for_request(given_request) page = WWW::Mechanize::File.new(URI.parse(given_request.uri), request.response_headers, request.body, request.status) raise WWW::Mechanize::ResponseCodeError.new(page) if request.status != 200 @@ -37,23 +53,9 @@ def search_for_error_request(given_request) @errors.max_by {|request| request.match(given_request)} end - def assert_queried(method, uri, params = {}) - request = Request.new(:method => method, :uri => uri, :request_headers => params) - @history.any? {|history_query| history_query == request} - end - - [:head, :get, :post, :put, :delete].each do |method| - module_eval <<-EOE, __FILE__, __LINE__ - def was_#{method}?(uri, params = {}) - assert_queried(:#{method}, uri, params) - end - EOE - end - - protected def reset_responses! @responses.clear end end # Agent end # FakeMechanize \ No newline at end of file