From f6510703e0891b516a783c9ee51059cac2156d58 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Fri, 21 Jun 2013 17:29:48 -0700 Subject: [PATCH] fixes #1792 --- app/views/comment_votes/create.json.erb | 5 +++ .../comment_votes_controller_test.rb | 39 ++++++++++++++----- 2 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 app/views/comment_votes/create.json.erb diff --git a/app/views/comment_votes/create.json.erb b/app/views/comment_votes/create.json.erb new file mode 100644 index 000000000..4b0d6500f --- /dev/null +++ b/app/views/comment_votes/create.json.erb @@ -0,0 +1,5 @@ +<% if @comment_vote.errors.any? %> + {"success": false, "errors": <%= @comment_vote.errors.full_messages.to_json.html_safe %>} +<% else %> + {"success": true} +<% end %> diff --git a/test/functional/comment_votes_controller_test.rb b/test/functional/comment_votes_controller_test.rb index 7f9ef7960..62d689d04 100644 --- a/test/functional/comment_votes_controller_test.rb +++ b/test/functional/comment_votes_controller_test.rb @@ -14,18 +14,39 @@ class CommentVotesControllerTest < ActionController::TestCase CurrentUser.ip_addr = nil end - should "create a vote" do - assert_difference("CommentVote.count", 1) do - post :create, {:format => "js", :comment_id => @comment.id, :score => 1}, {:user_id => @user.id} - assert_response :success + context "#create.json" do + should "create a vote" do + assert_difference("CommentVote.count", 1) do + post :create, {:format => "json", :comment_id => @comment.id, :score => 1}, {:user_id => @user.id} + assert_response :success + assert_equal("{\"success\": true}", @response.body.strip) + end + end + + should "fail silently on errors" do + FactoryGirl.create(:comment_vote, :comment => @comment) + assert_difference("CommentVote.count", 0) do + post :create, {:format => "json", :comment_id => @comment.id, :score => 1}, {:user_id => @user.id} + assert_response 422 + assert_equal("{\"errors\":{\"user_id\":[\"have already voted for this comment\"]}}", @response.body.strip) + end end end - should "fail silently on errors" do - FactoryGirl.create(:comment_vote, :comment => @comment) - assert_difference("CommentVote.count", 0) do - post :create, {:format => "js", :comment_id => @comment.id, :score => 1}, {:user_id => @user.id} - assert_response :success + context "#create.js" do + should "create a vote" do + assert_difference("CommentVote.count", 1) do + post :create, {:format => "js", :comment_id => @comment.id, :score => 1}, {:user_id => @user.id} + assert_response :success + end + end + + should "fail silently on errors" do + FactoryGirl.create(:comment_vote, :comment => @comment) + assert_difference("CommentVote.count", 0) do + post :create, {:format => "js", :comment_id => @comment.id, :score => 1}, {:user_id => @user.id} + assert_response :success + end end end end