All tests passing now. Yay!

This commit is contained in:
Bryan Helmkamp 2008-04-07 18:48:45 +01:00
parent effc446daf
commit 7afa0f0561
5 changed files with 122 additions and 97 deletions

View File

@ -177,11 +177,28 @@ module Webrat
class RadioField < Field
def to_param
return nil if @value.nil?
super
end
def choose
other_options.each do |option|
option.unset
end
set(@element["value"] || "on")
end
protected
def other_options
@form.fields.select { |f| f.name == name }
end
def default_value
if @element["checked"] == "checked"
@element["value"]
@element["value"] || "on"
else
nil
end

View File

@ -28,6 +28,18 @@ module Webrat
nil
end
def fields
return @fields if @fields
@fields = []
(@element / "input, textarea, select").each do |field_element|
@fields << Field.class_for_element(field_element).new(self, field_element)
end
@fields
end
def submit
Page.new(@page.session, form_action, form_method, params)
@ -65,18 +77,6 @@ module Webrat
fields.select { |f| field_types.include?(f.class) }
end
def fields
return @fields if @fields
@fields = []
(@element / "input, textarea, select").each do |field_element|
@fields << Field.class_for_element(field_element).new(self, field_element)
end
@fields
end
def params
all_params = {}

View File

@ -58,11 +58,9 @@ module Webrat
#
# Example:
# chooses 'First Option'
def chooses(field)
radio = find_field_by_name_or_label(field)
flunk("Could not find radio button #{field.inspect}") if radio.nil?
flunk("Input #{radio.inspect} is not a radio button") unless radio['type'] == 'radio'
add_form_data(radio, radio["value"] || "on")
def chooses(label)
field = find_field(label, RadioField)
field.choose
end
# Verifies that a an option element exists on the current page with the specified

View File

@ -7,7 +7,6 @@ module Webrat
end
def matches_text?(text)
# require "rubygems"; require "ruby-debug"; Debugger.start; debugger
@element.innerHTML =~ /^\W*#{Regexp.escape(text.to_s)}\b/i
end

View File

@ -1,78 +1,89 @@
# require File.dirname(__FILE__) + "/helper"
#
# class ChoosesTest < Test::Unit::TestCase
#
# def setup
# @session = ActionController::Integration::Session.new
# @session.stubs(:assert_response)
# @session.stubs(:get_via_redirect)
# @session.stubs(:response).returns(@response=mock)
# end
#
# def test_should_fail_if_no_radio_buttons_found
# @response.stubs(:body).returns(<<-EOS)
# <form method="post" action="/login">
# </form>
# EOS
#
# assert_raises RuntimeError do
# @session.chooses "first option"
# end
# end
#
# def test_should_fail_if_input_is_not_a_radio_button
# @response.stubs(:body).returns(<<-EOS)
# <form method="post" action="/login">
# <input type="text" name="first_option" />
# </form>
# EOS
#
# assert_raises RuntimeError do
# @session.chooses "first_option"
# end
# end
#
# def test_should_check_rails_style_radio_buttons
# @response.stubs(:body).returns(<<-EOS)
# <form method="get" action="/login">
# <input id="user_gender_male" name="user[gender]" type="radio" value="M" />
# <label for="user_gender_male">Male</label>
# <input id="user_gender_female" name="user[gender]" type="radio" value="F" />
# <label for="user_gender_female">Female</label>
# <input type="submit" />
# </form>
# EOS
# @session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "M"})
# @session.chooses "Male"
# @session.clicks_button
# end
#
# def test_should_only_submit_last_chosen_value
# @response.stubs(:body).returns(<<-EOS)
# <form method="get" action="/login">
# <input id="user_gender_male" name="user[gender]" type="radio" value="M" />
# <label for="user_gender_male">Male</label>
# <input id="user_gender_female" name="user[gender]" type="radio" value="F" />
# <label for="user_gender_female">Female</label>
# <input type="submit" />
# </form>
# EOS
# @session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "M"})
# @session.chooses "Female"
# @session.chooses "Male"
# @session.clicks_button
# end
#
# def test_should_result_in_the_value_on_being_posted_if_not_specified
# @response.stubs(:body).returns(<<-EOS)
# <form method="post" action="/login">
# <input type="radio" name="first_option" />
# <input type="submit" />
# </form>
# EOS
# @session.expects(:post_via_redirect).with("/login", "first_option" => "on")
# @session.chooses "first_option"
# @session.clicks_button
# end
#
# end
require File.dirname(__FILE__) + "/helper"
class ChoosesTest < Test::Unit::TestCase
def setup
@session = ActionController::Integration::Session.new
@session.stubs(:assert_response)
@session.stubs(:get_via_redirect)
@session.stubs(:response).returns(@response=mock)
end
def test_should_fail_if_no_radio_buttons_found
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login">
</form>
EOS
assert_raises RuntimeError do
@session.chooses "first option"
end
end
def test_should_fail_if_input_is_not_a_radio_button
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login">
<input type="text" name="first_option" />
</form>
EOS
assert_raises RuntimeError do
@session.chooses "first_option"
end
end
def test_should_check_rails_style_radio_buttons
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<input id="user_gender_male" name="user[gender]" type="radio" value="M" />
<label for="user_gender_male">Male</label>
<input id="user_gender_female" name="user[gender]" type="radio" value="F" />
<label for="user_gender_female">Female</label>
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "M"})
@session.chooses "Male"
@session.clicks_button
end
def test_should_only_submit_last_chosen_value
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<input id="user_gender_male" name="user[gender]" type="radio" value="M" />
<label for="user_gender_male">Male</label>
<input id="user_gender_female" name="user[gender]" type="radio" value="F" />
<label for="user_gender_female">Female</label>
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "M"})
@session.chooses "Female"
@session.chooses "Male"
@session.clicks_button
end
def test_should_result_in_the_value_on_being_posted_if_not_specified
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login">
<input type="radio" name="first_option" />
<input type="submit" />
</form>
EOS
@session.expects(:post_via_redirect).with("/login", "first_option" => "on")
@session.chooses "first_option"
@session.clicks_button
end
def test_should_result_in_the_value_on_being_posted_if_not_specified_and_checked_by_default
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login">
<input type="radio" name="first_option" checked="checked"/>
<input type="submit" />
</form>
EOS
@session.expects(:post_via_redirect).with("/login", "first_option" => "on")
@session.clicks_button
end
end