Fix support for passing in an ActiveRecord model to within (which uses dom_id) LB/BH

This commit is contained in:
Bryan Helmkamp 2009-09-18 11:28:07 -04:00
parent 862471476f
commit b51ba029db
7 changed files with 40 additions and 39 deletions

View File

@ -11,21 +11,6 @@ module Webrat
@integration_session = session
end
# The Rails version of within supports passing in a model and Webrat
# will apply a scope based on Rails' dom_id for that model.
#
# Example:
# within User.last do
# click_link "Delete"
# end
def within(selector_or_object, &block)
if selector_or_object.is_a?(String)
super
else
super('#' + dom_id(selector_or_object), &block)
end
end
def doc_root
File.expand_path(File.join(RAILS_ROOT, 'public'))
end

View File

@ -5,5 +5,21 @@ module ActionController #:nodoc:
IntegrationTest.class_eval do
include Webrat::Methods
include Webrat::Matchers
# The Rails version of within supports passing in a model and Webrat
# will apply a scope based on Rails' dom_id for that model.
#
# Example:
# within User.last do
# click_link "Delete"
# end
def within(selector_or_object, &block)
if selector_or_object.is_a?(String)
super
else
super('#' + RecordIdentifier.dom_id(selector_or_object), &block)
end
end
end
end

View File

@ -40,4 +40,7 @@ class WebratController < ApplicationController
render :text => params.to_json
end
def within
end
end

View File

@ -0,0 +1,3 @@
<div id="new_object">
<a href="/">Edit Object</a>
</div>

View File

@ -12,6 +12,7 @@ ActionController::Routing::Routes.draw do |map|
webrat.before_redirect_form "/before_redirect_form", :action => "before_redirect_form"
webrat.redirect_to_show_params "/redirect_to_show_params", :action => "redirect_to_show_params"
webrat.show_params "/show_params", :action => "show_params"
webrat.within "/within", :action => "within"
webrat.root :action => "form"
end

View File

@ -89,6 +89,23 @@ class WebratTest < ActionController::IntegrationTest
assert_have_selector "h1"
end
test "should accept an Object argument to #within and translate using dom_id" do
webrat.simulate do
visit within_path
object = Object.new
def object.id
nil
end
within(object) do
click_link "Edit Object"
end
assert_contain "Webrat Form"
end
end
# Firefox detects and prevents infinite redirects under Selenium
unless ENV['WEBRAT_INTEGRATION_MODE'] == 'selenium'
test "should detect infinite redirects" do

View File

@ -83,28 +83,4 @@ describe Webrat::RailsAdapter do
it "should provide a doc_root" do
Webrat::RailsAdapter.new(mock("integration session")).should respond_to(:doc_root)
end
it "should accept an ActiveRecord argument to #within and translate to a selector using dom_id" do
pending "Move this to spec/public/within_spec.rb"
body = <<-HTML
<a href="/page1">Edit</a>
<div id="new_object">
<a href="/page2">Edit</a>
</div>
HTML
response = mock("response", :body => body, :headers => {}, :code => 200)
@integration_session.stub!(:response => response)
@integration_session.should_receive(:get).with("/page2", {}, nil)
rails_session = Webrat::RailsAdapter.new(@integration_session)
object = Object.new
object.stub!(:id => nil)
rails_session.within(object) do
rails_session.click_link 'Edit'
end
end
end