require File.dirname(__FILE__) + '/test_helper' class WebService attr_accessor :output def get(url) @output end def with_hash(what, hash) @output end end class CacheTest < Test::Unit::TestCase def teardown ENV.delete 'CACHE_RESULT' ENV.delete 'NO_CACHE' end def test_cache_result_of_with_cache WebService.cache_result_of :get web = WebService.new web.output = 'uncached' assert_equal 'cached', web.get('http://macournoyer.wordpress.com') end def test_cache_result_of_with_no_cache ENV['NO_CACHE'] = 'true' WebService.cache_result_of :get web = WebService.new web.output = 'uncached' assert_equal 'uncached', web.get('http://macournoyer.wordpress.com') end def test_cache_result_writes_to_file ENV['CACHE_RESULT'] = 'true' WebService.cache_result_of :get web = WebService.new web.output = 'uncached' File.expects(:open).once.with("#{WebService.fixture_path}/web_service/http---macournoyer-wordpress-com", 'w').yields({}) assert_equal 'uncached', web.get('http://macournoyer.wordpress.com') end def test_fixture_file_name_with_hash_argument ENV['CACHE_RESULT'] = 'true' WebService.cache_result_of :with_hash web = WebService.new web.output = 'uncached' File.expects(:open).once.with("#{WebService.fixture_path}/web_service/hi-a-a-z-z", 'w').yields({}) assert_equal 'uncached', web.with_hash('hi', :z => 'z', :a => 'a') end end