FakeMechanize provides methods and classes to write offline tests for applications which relies on Mechanize. It replace Mechanize and emulate an agent which aim to act like a real Mechanize Agent. Fake agent accepts options to define queries and their answers.
-
Simple examples to test a basic api
# Initialize a fake agent @http_agent = FakeMechanize::Agent.new # Create answers and their params @http_agent.respond_to do |mock| # Answers to get queries to http://api.example.com/users/count?group=students # with the string "42" mock.get :uri => "http://api.example.com/users/count", :parameters => {:group => "students"}, :body => "42" # Answers to post queries to http://api.example.com/users/3/activate # with the string "true" mock.post :uri => "http://api.example.com/users/3/activate", :body => "true" # Answers to post queries to http://api.example.com/users/authentify # with the string "Qt5c1HWwCXDhKskMrBqMdQ". # This example could be an authentication process mock.post :uri => "http://api.example.com/users/authentify", :parameters => {:Email => 'jack@bauer.com', :Passwd => 'secure'}, :body => "Qt5c1HWwCXDhKskMrBqMdQ" # Will reply with an http error code of 403 with no body if the post query # to http://api.example.com/users/authentify does not have specified parameters. mock.error :uri => "http://api.example.com/users/authentify", :params_not_equal => {:Email => 'jack@bauer.com', :Passwd => 'secure'}, :method => :post, :status => 403 end # Now you can use this agent like a real one r = mock.get("http://api.example.com/users/count", :group => "students") # => WWW::Mechanize::File r.body # => "42" # Posting r = mock.post("http://api.example.com/users/authentify", :Email => "jack@bauer.com", :Passwd => "secure") r.status # => 200 r.body # => "Qt5c1HWwCXDhKskMrBqMdQ" # Handling errors r = mock.post("http://api.example.com/users/authentify", :Email => "jack@bauer.com", :Passwd => "bad") r.status # => 403 r.body # => nil
-
mechanize gem
gem install fake-mechanize
(The MIT License)
Copyright © 2009, Fabien Jakimowicz <fabien@jakimowicz.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.