Norm, that was a huge help! Thanks! I thought I'd post accurate functional test files for chapter 9, as well, as I ran across similar, though many more, issues there.
account_controller_test.rb
Code:
require File.dirname(__FILE__) + '/../test_helper'
require 'account_controller'
# Re-raise errors caught by the controller.
class AccountController; def rescue_action(e) raise e end; end
class AccountControllerTest < Test::Unit::TestCase
fixtures :users, :stories, :votes
def setup
@controller = AccountController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
def test_should_show_login_form
get :login
assert_response :success
assert_template 'login'
assert_select 'form p', 4
end
def test_should_perform_user_login
post :login, :login => 'patrick', :password => 'sekrit'
assert_redirected_to :controller => 'story'
assert_equal users(:patrick).id, session[:user_id]
assert_equal users(:patrick), assigns['current_user']
end
def test_should_fail_user_login
post :login, :login => 'no such', :password => 'user'
assert_response :success
assert_template 'login'
assert_nil session[:user_id]
end
def test_should_redirect_after_login_with_return_url
post :login, { :login => 'patrick', :password => 'sekrit' },
{ 'return_to' => '/story/new' }
assert_redirected_to '/story/new'
end
def test_should_logout_and_clear_session
post :login, :login => 'patrick', :password => 'sekrit'
assert_not_nil assigns['current_user']
assert_not_nil session[:user_id]
get :logout
assert_response :success
assert_template 'logout'
assert_select 'h2', 'Logout successful'
assert_nil assigns['current_user']
assert_nil session[:user_id]
end
def test_should_show_user
get :show, :id => 'patrick'
assert_response :success
assert_template 'show'
assert_equal users(:patrick), assigns['user']
end
def test_should_show_user
get :show, :id => 'patrick'
assert_response :success
assert_template 'show'
assert_equal users(:patrick), assigns['user']
end
def test_should_show_submitted_stories
get :show, :id => 'patrick'
assert_select 'div#stories_submitted div.story', :count => 2
end
def test_should_show_stories_voted_on
get :show, :id => 'patrick'
assert_select 'div#stories_voted_on div.story', :count => 1
end
end
story_controller_test.rb
Code:
require File.dirname(__FILE__) + '/../test_helper'
require 'story_controller'
# Re-raise errors caught by the controller.
class StoryController; def rescue_action(e) raise e end; end
class StoryControllerTest < Test::Unit::TestCase
fixtures :stories, :votes, :users
def setup
@controller = StoryController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
def test_should_show_index
get :index
assert_response :success
assert_template 'index'
assert_not_nil assigns['story']
end
def test_should_show_new
get_with_user :new
assert_response :success
assert_template 'new'
assert_not_nil assigns['story']
end
def test_should_show_new_form
get_with_user :new
assert_select 'form p', :count => 4
end
def test_should_add_story
post_with_user :new, :story => {
:name => 'test story',
:link => 'http://www.test.com/'
}
assert ! assigns['story'].new_record?
assert_redirected_to :action => 'index'
assert_not_nil flash[:notice]
end
def test_should_reject_missing_story_attribute
post_with_user :new, :story => {
:name => 'story without a link'
}
assert assigns['story'].errors.on(:link)
end
def test_should_show_story
get :show, :permalink => 'my-shiny-weblog'
assert_response :success
assert_template 'show'
assert_equal stories(:first), assigns['story']
end
def test_should_show_story_vote_elements
get_with_user :show, :permalink => 'my-shiny-weblog'
assert_select 'h2 span#vote_score'
assert_select 'ul#vote_history li', :count => 2
assert_select 'div#vote_link'
end
def test_should_accept_vote
assert stories(:another).votes.empty?
post_with_user :vote, :id => 2
assert ! assigns['story'].reload.votes.empty?
end
def test_should_render_rjs_after_vote_with_ajax
xml_http_request :post_with_user, :vote, :id => 2
assert_response :success
assert_template 'vote'
end
def test_should_redirect_after_vote_with_get
get_with_user :vote, :id => 2
assert_redirected_to :action => 'show',
:permalink => 'sitepoint-forums'
end
def test_should_show_story_submitter
get :show, :permalink => 'my-shiny-weblog'
assert_select 'p.submitted_by span', 'patrick'
end
def test_should_indicate_not_logged_in
get :index
assert_select 'div#login_logout em', 'Not logged in.'
end
def test_should_show_navigation_menu
get :index
assert_select 'ul#navigation li', 3
end
def test_should_indicate_logged_in_user
get_with_user :index
assert_equal users(:patrick), assigns['current_user']
assert_select 'div#login_logout em a', '(Logout)'
end
def test_should_redirect_if_not_logged_in
get :new
assert_response :redirect
assert_redirected_to '/account/login'
end
def test_should_store_user_with_story
post_with_user :new, :story => {
:name => 'story with user',
:link => 'http://www.story-with-user.com/'
}
assert_equal users(:patrick), assigns['story'].user
end
def test_should_show_index
get :index
assert_response :success
assert_template 'index'
end
def test_should_show_bin
get :bin
assert_response :success
assert_template 'index'
end
def test_should_only_list_promoted_on_index
get :index
assert_equal [ stories(:promoted) ], assigns['stories']
end
def test_should_only_list_unpromoted_in_bin
get :bin
assert_equal [ stories(:another), stories(:first) ],
assigns['stories']
end
def test_should_use_story_index_as_default
assert_routing '', :controller => 'story', :action => 'index'
end
def test_should_show_story_on_index
get :index
assert_select 'h2', 'Showing 1 front page story'
assert_select 'div#content div.story', :count => 1
end
def test_should_show_stories_in_bin
get :bin
assert_select 'h2', 'Showing 2 upcoming stories'
assert_select 'div#content div.story', :count => 2
end
def test_should_store_user_with_vote
post_with_user :vote, :id => 2
assert_equal users(:patrick), assigns['story'].votes.last.user
end
def test_should_not_show_vote_button_if_not_logged_in
get :show, :permalink => 'my-shiny-weblog'
assert_select 'div#vote_link', false
end
def test_should_show_story_submitter
get :show, :permalink => 'promoted-story'
assert_select 'p.submitted_by a', 'john'
end
protected
def get_with_user(action, parameters = nil, session = nil, flash = nil)
get action, parameters, { 'user_id' => users(:patrick).id }
end
def post_with_user(action, parameters = nil, session = nil, flash = nil)
post action, parameters, { 'user_id' => users(:patrick).id }
end
end
Bookmarks