Permalink
Browse files

Make sure same user can vote the same article only once.

  • Loading branch information...
1 parent 53723d4 commit 2a7fb397e97439fc46353d59a763b78599d314e2 @lvterry committed Apr 18, 2012
Showing with 25 additions and 11 deletions.
  1. +1 −0 app/models/vote.rb
  2. +0 −11 test/functional/votes_controller.test.rb
  3. +24 −0 test/functional/votes_controller_test.rb
View
@@ -1,4 +1,5 @@
class Vote < ActiveRecord::Base
belongs_to :user, :counter_cache => true
belongs_to :article, :counter_cache => true
+ validates :article_id, :uniqueness => { scope: :user_id }
end
@@ -1,11 +0,0 @@
-require 'test_helper'
-
-class VotesControllerTest < ActionController::TestCase
- test "should create a vote" do
- session[:user_id] ||= users(:one).id
- assert_difference 'Vote.count' do
- post :create, :article_id => articles(:one).id
- end
- assert_redirected_to root_url
- end
-end
@@ -0,0 +1,24 @@
+require 'test_helper'
+
+class VotesControllerTest < ActionController::TestCase
+ def setup
+ session[:user_id] ||= users(:dave).id
+ request.env["HTTP_REFERER"] = root_url
+ end
+
+ test "should create a vote" do
+ assert_difference 'Vote.count' do
+ post :create, :article_id => articles(:one).id
+ end
+ assert_redirected_to root_url
+ end
+
+ test "should not create vote for the same article and the same user twice" do
+ assert_difference 'Vote.count' do
+ post :create, :article_id => articles(:one).id
+ end
+ assert_difference 'Vote.count', 0 do
+ post :create, :article_id => articles(:one).id
+ end
+ end
+end

0 comments on commit 2a7fb39

Please sign in to comment.