1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'quickvote_controller'
4 class ActionController::TestSession
6 # Override this so we can set session ID
7 # pass in the 'test_session_id' session variable to override default
8 return "12345678" unless @attributes
9 if @attributes.has_key? 'test_session_id'
10 return @attributes['test_session_id']
14 # Re-raise errors caught by the controller.
15 class QuickvoteController; def rescue_action(e) raise e end; end
17 class QuickvoteControllerTest < Test::Unit::TestCase
19 @controller = QuickvoteController.new
20 @request = ActionController::TestRequest.new
21 @response = ActionController::TestResponse.new
24 # Replace this with your real tests.
30 def test_create_quickvote
31 post(:create, {'commit' =>"Create Quickvote", 'quickvote' =>{'name' =>"variable", 'description' =>"Favorite variable."}}, nil, {:candlist=>["foo", "bar", "foobar"]})
32 assert_template "quickvote/success"
33 get :index, { 'ident' => "variable"}
34 assert_response :success
37 def test_create_dupe_quickvote
39 assert_raise(Test::Unit::AssertionFailedError) do
44 def test_create_quickvote_badname
45 post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => ["foo", "bar", "foobar"]})
46 assert_template "quickvote/create"
49 def test_create_quickvote_dupe_candidate
50 post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => ["foo", "bar", "bar", "foobar"]})
51 assert_template "quickvote/create"
54 def test_create_quickvote_nil_candidate
55 post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => nil})
56 assert_template "quickvote/create"
57 post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => []})
58 assert_template "quickvote/create"
61 def test_get_quickvote_nonexistent
62 get :index, { 'ident' => "idontexist" }
63 assert_redirected_to :controller => 'site'
66 def test_get_result_empty_vote
68 get :results, { 'ident' => 'variable' }
69 assert_response :success
72 def test_get_result_nonexistent
74 get :results, { 'ident' => 'asdflaksdjf' }
75 assert_redirected_to :controller => 'site'
78 def test_get_result_with_a_vote
80 get :results, { 'ident' => 'variable' }
81 assert_response :success
84 def test_get_result_multi_votes
86 votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id}
88 get :index, { 'ident' => 'variable' }, { 'test_session_id' => (time+1)*50 }
89 assert_response :success
90 post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }, { 'test_session_id' => (time+1)*50 }
91 assert_template 'quickvote/thanks'
93 get :results, { 'ident' => 'variable' }
94 assert_response :success
97 def test_cast_quickvote
99 votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id}
100 post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }
101 assert_template 'quickvote/thanks'