Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Using time travel to unit test time-based data

Searching for a way to test time-based data in ruby, I came across an excellent suggestion from Rick Olson (technoweenie). I've cleaned it a little to make it clearer to read, but the main credit should go to Rick.

   1  
   2  class << Time
   3    unless method_defined? :now_before_time_travel
   4      alias_method :now_before_time_travel, :now
   5    end
   6    
   7    def now 
   8      @now || now_before_time_travel 
   9    end 
  10    
  11    def travel_to(time, &block)
  12      @now = time
  13      block.call
  14    ensure
  15      @now = nil
  16    end
  17  end


   1  
   2  def test_something_involving_time
   3    account = Time.travel_to(creation_time = Time.now) do
   4      Account.new  
   5    end
   6  
   7    assert_equal creation_time, account.creation_time
   8  end

testing for SSL in Ruby on Rails functional tests

   1  
   2  class AccountControllerTest < Test::Unit::TestCase
   3    def setup
   4      @controller = AccountController.new
   5      @request    = ActionController::TestRequest.new
   6      @request.env['HTTPS'] = 'on'
   7      @response   = ActionController::TestResponse.new
   8    end
   9  
  10    def test_ssl
  11      assert @request.ssl?
  12    end
  13  end

simulating cookies in RoR functional tests

Okay, my first try didn't work. This seems to though:
   1  
   2  @request.cookies['key'] = CGI::Cookie.new( 
   3    'name'   => 'key',
   4    'value'   => my_value,
   5    'expires' => 14.days.from_now,
   6    'path'    => '/',
   7    'domain'  => 'example.com'
   8  )


When you're simulating a cookie, you have to go all the way. It expects it as a CGI::Cookie, set by a hash with strings for keys.
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS