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
class Configuration
attr_accessor :mode
# Sets whether to save and open pages with error status codes in a browser
attr_accessor :open_error_files
def initialize
self.mode = :rails
self.open_error_files = true
end

View File

@ -5,7 +5,7 @@ module Webrat
meths.each do |meth|
self.class_eval <<-RUBY
def #{meth}(*args, &blk)
@_webrat_session ||= ::Webrat::MerbSession.new
@_webrat_session ||= ::Webrat.session_class.new(self)
@_webrat_session.#{meth}(*args, &blk)
end
RUBY
@ -38,7 +38,12 @@ module Webrat
:clicks_button, :click_button,
:reload, :reloads,
: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

View File

@ -7,17 +7,37 @@ module Webrat
class PageLoadError < WebratError
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
extend Forwardable
include Logging
attr_reader :current_url
def initialize #:nodoc:
def initialize(context = nil) #:nodoc:
@http_method = :get
@data = {}
@default_headers = {}
@custom_headers = {}
@context = context
end
# 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
alias :page :response
def initialize(mechanize = WWW::Mechanize.new)
super()
@mechanize = mechanize
end
def get(url, data, headers_argument_not_used = nil)
@response = @mechanize.get(url, data)
@response = mechanize.get(url, data)
end
def post(url, data, headers_argument_not_used = nil)
@ -25,11 +20,7 @@ module Webrat
end
memo
end
@response = @mechanize.post(url, post_data)
end
def response
@response
@response = mechanize.post(url, post_data)
end
def response_body
@ -40,7 +31,13 @@ module Webrat
@response.code.to_i
end
def_delegators :@mechanize, :basic_auth
def mechanize
@mechanize = WWW::Mechanize.new
end
def_delegators :mechanize, :basic_auth
end
end
Webrat.configuration.mode = :mechanize

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -27,10 +27,10 @@ describe Webrat::MechanizeSession do
end
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)
Webrat::MechanizeSession.new(mechanize).post(url, data)
Webrat::MechanizeSession.new.post(url, data)
end
end
end

View File

@ -1,12 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/helper')
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
response = mock("response", :body => "<html>")
integration_session = mock("integration session", :response => response)