Change Webrat Rails integration to use the Webrat::Methods module

This commit is contained in:
Bryan Helmkamp 2008-11-22 23:49:12 -05:00
parent a8e02a6b6e
commit adf68c2f8f
12 changed files with 70 additions and 58 deletions

View File

@ -12,10 +12,14 @@ module Webrat
end end
class Configuration class Configuration
attr_accessor :mode
# Sets whether to save and open pages with error status codes in a browser # Sets whether to save and open pages with error status codes in a browser
attr_accessor :open_error_files attr_accessor :open_error_files
def initialize def initialize
self.mode = :rails
self.open_error_files = true self.open_error_files = true
end end

View File

@ -5,7 +5,7 @@ module Webrat
meths.each do |meth| meths.each do |meth|
self.class_eval <<-RUBY self.class_eval <<-RUBY
def #{meth}(*args, &blk) def #{meth}(*args, &blk)
@_webrat_session ||= ::Webrat::MerbSession.new @_webrat_session ||= ::Webrat.session_class.new(self)
@_webrat_session.#{meth}(*args, &blk) @_webrat_session.#{meth}(*args, &blk)
end end
RUBY RUBY
@ -38,7 +38,12 @@ module Webrat
:clicks_button, :click_button, :clicks_button, :click_button,
:reload, :reloads, :reload, :reloads,
:clicks_link_within, :click_link_within, :clicks_link_within, :click_link_within,
:field_labeled :field_labeled,
:set_hidden_field, :submit_form,
:request_page, :current_dom,
:selects_date, :selects_time, :selects_datetime,
:select_date, :select_time, :select_datetime
end end
end end

View File

@ -7,17 +7,37 @@ module Webrat
class PageLoadError < WebratError class PageLoadError < WebratError
end end
def self.session_class
case Webrat.configuration.mode
when :rails
RailsSession
when :merb
MerbSession
when :selenium
SeleniumSession
when :rack
RackSession
when :sinatra
SinatraSession
when :mechanize
MechanizeSession
else
raise WebratError.new("Unknown Webrat mode: #{Webrat.configuration.mode.inspect}")
end
end
class Session class Session
extend Forwardable extend Forwardable
include Logging include Logging
attr_reader :current_url attr_reader :current_url
def initialize #:nodoc: def initialize(context = nil) #:nodoc:
@http_method = :get @http_method = :get
@data = {} @data = {}
@default_headers = {} @default_headers = {}
@custom_headers = {} @custom_headers = {}
@context = context
end end
# Saves the page out to RAILS_ROOT/tmp/ and opens it in the default # Saves the page out to RAILS_ROOT/tmp/ and opens it in the default

View File

@ -6,13 +6,8 @@ module Webrat
attr_accessor :response attr_accessor :response
alias :page :response alias :page :response
def initialize(mechanize = WWW::Mechanize.new)
super()
@mechanize = mechanize
end
def get(url, data, headers_argument_not_used = nil) def get(url, data, headers_argument_not_used = nil)
@response = @mechanize.get(url, data) @response = mechanize.get(url, data)
end end
def post(url, data, headers_argument_not_used = nil) def post(url, data, headers_argument_not_used = nil)
@ -25,11 +20,7 @@ module Webrat
end end
memo memo
end end
@response = @mechanize.post(url, post_data) @response = mechanize.post(url, post_data)
end
def response
@response
end end
def response_body def response_body
@ -40,7 +31,13 @@ module Webrat
@response.code.to_i @response.code.to_i
end end
def_delegators :@mechanize, :basic_auth def mechanize
@mechanize = WWW::Mechanize.new
end
def_delegators :mechanize, :basic_auth
end end
end end
Webrat.configuration.mode = :mechanize

View File

@ -73,3 +73,5 @@ class Merb::Test::RspecStory #:nodoc:
end end
end end
Webrat.configuration.mode = :merb

View File

@ -22,3 +22,5 @@ module Webrat
end end
end end
end end
Webrat.configuration.mode = :rack

View File

@ -1,11 +1,6 @@
module Webrat module Webrat
class RailsSession < Session #:nodoc: class RailsSession < Session #:nodoc:
def initialize(integration_session)
super()
@integration_session = integration_session
end
def doc_root def doc_root
File.expand_path(File.join(RAILS_ROOT, 'public')) File.expand_path(File.join(RAILS_ROOT, 'public'))
end end
@ -40,9 +35,13 @@ module Webrat
protected protected
def integration_session
@context
end
def do_request(http_method, url, data, headers) #:nodoc: def do_request(http_method, url, data, headers) #:nodoc:
update_protocol(url) update_protocol(url)
@integration_session.request_via_redirect(http_method, remove_protocol(url), data, headers) integration_session.request_via_redirect(http_method, remove_protocol(url), data, headers)
end end
def remove_protocol(href) #:nodoc: def remove_protocol(href) #:nodoc:
@ -55,14 +54,14 @@ module Webrat
def update_protocol(href) #:nodoc: def update_protocol(href) #:nodoc:
if href =~ /^https:/ if href =~ /^https:/
@integration_session.https!(true) integration_session.https!(true)
elsif href =~ /^http:/ elsif href =~ /^http:/
@integration_session.https!(false) integration_session.https!(false)
end end
end end
def response #:nodoc: def response #:nodoc:
@integration_session.response integration_session.response
end end
end end
@ -71,30 +70,14 @@ end
module ActionController module ActionController
module Integration module Integration
class Session #:nodoc: class Session #:nodoc:
unless instance_methods.include?("put_via_redirect") unless instance_methods.include?("put_via_redirect")
require "webrat/rails/redirect_actions" require "webrat/rails/redirect_actions"
include Webrat::RedirectActions include Webrat::RedirectActions
end end
def respond_to?(name) include Webrat::Methods
super || webrat_session.respond_to?(name)
end end
def method_missing(name, *args, &block)
if webrat_session.respond_to?(name)
webrat_session.send(name, *args, &block)
else
super
end end
end end
protected Webrat.configuration.mode = :rails
def webrat_session
@webrat_session ||= Webrat::RailsSession.new(self)
end
end
end
end

View File

@ -2,3 +2,4 @@ gem "selenium-client", ">=1.2.9"
require "selenium/client" require "selenium/client"
require "webrat/selenium/selenium_session" require "webrat/selenium/selenium_session"
Webrat.configuration.mode = :selenium

View File

@ -17,3 +17,5 @@ module Webrat
end end
end end
Webrat.configuration.mode = :sinatra

View File

@ -9,11 +9,12 @@ describe Webrat::Configuration do
Webrat.reset_for_test Webrat.reset_for_test
end end
it "should have a default config" do it "should default to Rails mode" do
Webrat.configuration.should be_an_instance_of(Webrat::Configuration) config = Webrat.configuration
config.mode.should == :rails
end end
it "should set default values" do it "should open error files by default" do
config = Webrat.configuration config = Webrat.configuration
config.open_error_files.should == true config.open_error_files.should == true
end end
@ -22,6 +23,7 @@ describe Webrat::Configuration do
Webrat.configure do |config| Webrat.configure do |config|
config.open_error_files = false config.open_error_files = false
end end
config = Webrat.configuration config = Webrat.configuration
config.open_error_files.should == false config.open_error_files.should == false
end end

View File

@ -27,10 +27,10 @@ describe Webrat::MechanizeSession do
end end
it "should flatten model post data" do it "should flatten model post data" do
mechanize = mock :mechanize mechanize = mock(:mechanize)
WWW::Mechanize.stub!(:new => mechanize)
mechanize.should_receive(:post).with(url, flattened_data) mechanize.should_receive(:post).with(url, flattened_data)
Webrat::MechanizeSession.new.post(url, data)
Webrat::MechanizeSession.new(mechanize).post(url, data)
end end
end end
end end

View File

@ -1,12 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/helper') require File.expand_path(File.dirname(__FILE__) + '/helper')
describe Webrat::RailsSession do describe Webrat::RailsSession do
it "should require a Rails Integration session to be initialized" do
lambda {
Webrat::RailsSession.new
}.should raise_error
end
it "should delegate response_body to the session response body" do it "should delegate response_body to the session response body" do
response = mock("response", :body => "<html>") response = mock("response", :body => "<html>")
integration_session = mock("integration session", :response => response) integration_session = mock("integration session", :response => response)