webrat/test/selects_test.rb

112 lines
3.8 KiB
Ruby
Raw Normal View History

2008-03-02 20:14:52 +00:00
require File.dirname(__FILE__) + "/helper"
class SelectsTest < 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_option_not_found
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<select name="month"><option value="1">January</option></select>
</form>
EOS
2008-04-07 04:52:49 +00:00
assert_raises RuntimeError do
@session.selects "February", :from => "month"
end
2008-03-02 20:14:52 +00:00
end
def test_should_fail_if_option_not_found_in_list_specified_by_element_name
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<select name="month"><option value="1">January</option></select>
<select name="year"><option value="2008">2008</option></select>
</form>
EOS
2008-04-07 04:52:49 +00:00
assert_raises RuntimeError do
@session.selects "February", :from => "year"
end
2008-03-02 20:14:52 +00:00
end
def test_should_fail_if_specified_list_not_found
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<select name="month"><option value="1">January</option></select>
</form>
EOS
2008-04-07 04:52:49 +00:00
assert_raises RuntimeError do
@session.selects "February", :from => "year"
end
2008-03-02 20:14:52 +00:00
end
def test_should_send_value_from_option
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login">
<select name="month"><option value="1">January</option></select>
<input type="submit" />
</form>
EOS
@session.expects(:post_via_redirect).with("/login", "month" => "1")
2008-04-07 04:52:49 +00:00
@session.selects "January", :from => "month"
2008-03-02 20:14:52 +00:00
@session.clicks_button
end
def test_should_work_without_specifying_the_field_name_or_label
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login">
<select name="month"><option value="1">January</option></select>
<input type="submit" />
</form>
EOS
@session.expects(:post_via_redirect).with("/login", "month" => "1")
@session.selects "January"
@session.clicks_button
end
2008-03-02 20:14:52 +00:00
def test_should_send_value_from_option_in_list_specified_by_name
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login">
<select name="start_month"><option value="s1">January</option></select>
<select name="end_month"><option value="e1">January</option></select>
<input type="submit" />
</form>
EOS
@session.expects(:post_via_redirect).with("/login", "start_month" => "s1", "end_month" => "e1")
2008-03-02 20:14:52 +00:00
@session.selects "January", :from => "end_month"
@session.clicks_button
end
def test_should_send_value_from_option_in_list_specified_by_label
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login">
<label for="start_month">Start Month</label>
<select id="start_month" name="start_month"><option value="s1">January</option></select>
<label for="end_month">End Month</label>
<select id="end_month" name="end_month"><option value="e1">January</option></select>
<input type="submit" />
</form>
EOS
@session.expects(:post_via_redirect).with("/login", "start_month" => "s1", "end_month" => "e1")
2008-03-02 20:14:52 +00:00
@session.selects "January", :from => "End Month"
@session.clicks_button
end
def test_should_use_option_text_if_no_value
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login">
<select name="month"><option>January</option></select>
<input type="submit" />
</form>
EOS
@session.expects(:post_via_redirect).with("/login", "month" => "January")
2008-04-07 04:52:49 +00:00
@session.selects "January", :from => "month"
2008-03-02 20:14:52 +00:00
@session.clicks_button
end
end