Skip to content

Commit

Permalink
Added new tests for votes and stories
Browse files Browse the repository at this point in the history
Added unit tests to the Story and Vote model to check the association
between the models. Added functional tests for the Stories and Votes
controller to ensure correct functionality for creating votes and
displaying votes.
  • Loading branch information
Jamestown committed Oct 13, 2009
1 parent 102a549 commit 606f1fe
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
8 changes: 4 additions & 4 deletions test/fixtures/votes.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

one:
story_id: 1
first:
story: one

two:
story_id: 1
second:
story: one
14 changes: 14 additions & 0 deletions test/functional/stories_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,18 @@ def test_should_reject_missing_story_attribute_link
assert_equal 'Story', story.class.to_s
assert story.errors.on(:link)
end

def test_should_show_story
get :show, :id => stories(:one)
assert_response :success
assert_template 'show'
assert_equal stories(:one), assigns(:story)
end

def test_should_show_story_vote_elements
get :show, :id => stories(:one)
assert_select 'h2 span#vote_score'
assert_select 'ul#vote_history li', :count => 2
assert_select 'div#vote_form form'
end
end
18 changes: 15 additions & 3 deletions test/functional/votes_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
require 'test_helper'

class VotesControllerTest < ActionController::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
def test_should_accept_vote
assert stories(:two).votes.empty?
post :create, :story_id => stories(:two)
assert !assigns(:story).votes.empty?
end

def test_should_render_rjs_after_vote_with_ajax
xml_http_request :post, :create, :story_id => stories(:two)
assert_response :success
assert_template 'create'
end

def test_should_redirect_after_vote_with_http_post
post :create, :story_id => stories(:two)
assert_redirected_to story_path(stories(:two))
end
end
13 changes: 13 additions & 0 deletions test/unit/story_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,17 @@ def test_should_create_story

assert s.valid?
end

def test_should_have_a_votes_association
assert_equal [votes(:first), votes(:second)], stories(:one).votes
end

def test_should_return_highest_vote_id_first
assert_equal votes(:second), stories(:one).votes.latest.first
end

def test_should_return_3_latest_votes
10.times { stories(:one).votes.create }
assert_equal 3, stories(:one).votes.latest.size
end
end
5 changes: 2 additions & 3 deletions test/unit/vote_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
require 'test_helper'

class VoteTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
def test_story_association
assert_equal stories(:one), votes(:first).story
end
end

0 comments on commit 606f1fe

Please sign in to comment.