1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

use mocha for TimeZone mocking in Form Options helper tests

This commit is contained in:
rick 2008-07-02 11:09:10 -07:00
parent 820992c98f
commit a4138d4632

View file

@ -1,26 +1,9 @@
require 'abstract_unit'
class MockTimeZone
attr_reader :name
TZInfo::Timezone.cattr_reader :loaded_zones
def initialize( name )
@name = name
end
def self.all
[ "A", "B", "C", "D", "E" ].map { |s| new s }
end
def ==( z )
z && @name == z.name
end
def to_s
@name
end
end
class FormOptionsHelperTest < ActionView::TestCase
uses_mocha "FormOptionsHelperTest" do
class FormOptionsHelperTest < ActionView::TestCase
tests ActionView::Helpers::FormOptionsHelper
silence_warnings do
@ -29,8 +12,15 @@ class FormOptionsHelperTest < ActionView::TestCase
Country = Struct.new('Country', :country_id, :country_name)
Firm = Struct.new('Firm', :time_zone)
Album = Struct.new('Album', :id, :title, :genre)
end
ActiveSupport::TimeZone = MockTimeZone
def setup
@fake_timezones = %w(A B C D E).inject([]) do |zones, id|
tz = TZInfo::Timezone.loaded_zones[id] = stub(:name => id, :to_s => id)
ActiveSupport::TimeZone.stubs(:[]).with(id).returns(tz)
zones << tz
end
ActiveSupport::TimeZone.stubs(:all).returns(@fake_timezones)
end
def test_collection_options
@ -661,7 +651,7 @@ class FormOptionsHelperTest < ActionView::TestCase
<option value="Yemen">Yemen</option>
<option value="Zambia">Zambia</option>
<option value="Zimbabwe">Zimbabwe</option></select>
COUNTRIES
COUNTRIES
assert_dom_equal(expected_select[0..-2], country_select("post", "origin"))
end
@ -916,7 +906,7 @@ COUNTRIES
<option value="Yemen">Yemen</option>
<option value="Zambia">Zambia</option>
<option value="Zimbabwe">Zimbabwe</option></select>
COUNTRIES
COUNTRIES
assert_dom_equal(expected_select[0..-2], country_select("post", "origin", ["New Zealand", "Nicaragua"]))
end
@ -1171,7 +1161,7 @@ COUNTRIES
<option value="Yemen">Yemen</option>
<option value="Zambia">Zambia</option>
<option value="Zimbabwe">Zimbabwe</option></select>
COUNTRIES
COUNTRIES
assert_dom_equal(expected_select[0..-2], country_select("post", "origin", ["New Zealand", "Nicaragua"]))
end
@ -1300,10 +1290,11 @@ COUNTRIES
html
end
uses_mocha "time_zone_select_with_priority_zones_as_regexp" do
def test_time_zone_select_with_priority_zones_as_regexp
@firm = Firm.new("D")
MockTimeZone.any_instance.stubs(:=~).returns(true,false,false,true,false)
@fake_timezones.each_with_index do |tz, i|
tz.stubs(:=~).returns(i.zero? || i == 3)
end
html = time_zone_select("firm", "time_zone", /A|D/)
assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
@ -1316,7 +1307,6 @@ COUNTRIES
"</select>",
html
end
end
def test_time_zone_select_with_default_time_zone_and_nil_value
@firm = Firm.new()
@ -1345,4 +1335,5 @@ COUNTRIES
html
end
end
end