fix tests

This commit is contained in:
r888888888
2017-01-24 16:05:06 -08:00
parent fe86f40f92
commit 1207faa600
3 changed files with 8 additions and 6 deletions

View File

@@ -8,7 +8,7 @@ class CommentVotesController < ApplicationController
@comment_vote = @comment.vote!(params[:score]) @comment_vote = @comment.vote!(params[:score])
rescue CommentVote::Error, ActiveRecord::RecordInvalid => x rescue CommentVote::Error, ActiveRecord::RecordInvalid => x
@error = x @error = x
render status: 500 render status: 422
end end
def destroy def destroy
@@ -16,6 +16,6 @@ class CommentVotesController < ApplicationController
@comment.unvote! @comment.unvote!
rescue CommentVote::Error => x rescue CommentVote::Error => x
@error = x @error = x
render status: 500 render status: 422
end end
end end

View File

@@ -1,4 +1,6 @@
<% if @comment_vote.errors.any? %> <% if @error %>
{"success": false, "errors": <%= @error.to_s.to_json.html_safe %>}
<% elsif @comment_vote.errors.any? %>
{"success": false, "errors": <%= @comment_vote.errors.full_messages.to_json.html_safe %>} {"success": false, "errors": <%= @comment_vote.errors.full_messages.to_json.html_safe %>}
<% else %> <% else %>
{"success": true} {"success": true}

View File

@@ -28,7 +28,7 @@ class CommentVotesControllerTest < ActionController::TestCase
assert_difference("CommentVote.count", 0) do assert_difference("CommentVote.count", 0) do
post :create, {:format => "json", :comment_id => @comment.id, :score => -1}, {:user_id => @user.id} post :create, {:format => "json", :comment_id => @comment.id, :score => -1}, {:user_id => @user.id}
assert_response 422 assert_response 422
assert_equal("{\"errors\":{\"user_id\":[\"have already voted for this comment\"]}}", @response.body.strip) assert_equal("{\"success\": false, \"errors\": \"Validation failed: You have already voted for this comment\"}", @response.body.strip)
end end
end end
end end
@@ -41,11 +41,11 @@ class CommentVotesControllerTest < ActionController::TestCase
end end
end end
should "fail silently on errors" do should "fail on errors" do
FactoryGirl.create(:comment_vote, :comment => @comment, :score => -1) FactoryGirl.create(:comment_vote, :comment => @comment, :score => -1)
assert_difference("CommentVote.count", 0) do assert_difference("CommentVote.count", 0) do
post :create, {:format => "js", :comment_id => @comment.id, :score => -1}, {:user_id => @user.id} post :create, {:format => "js", :comment_id => @comment.id, :score => -1}, {:user_id => @user.id}
assert_response :success assert_response 422
end end
end end
end end