Bug: when moving favorites, the parent is given an upvote for each gold+
favoriter, but the magnitude of these upvotes was based on the
CurrentUser, rather than the upvoter. This meant that upvotes for
supervoter favorites weren't given correctly.
To fix this, `magnitude` is changed to use the voting `user` instead of
CurrentUser.
New problem: when setting the score, `score=` calls `magnitude`, but
`user` isn't initialized yet. So we also refactor so that
1. `initialize_attributes` initializes `user` before setting `score`
2. the vote direction is given by `vote`, so it's separate from `score`
3. updating the score on the post happens in a callback instead of
directly in `score=`.
32 lines
765 B
Ruby
32 lines
765 B
Ruby
require 'test_helper'
|
|
|
|
class PostVoteTest < ActiveSupport::TestCase
|
|
def setup
|
|
super
|
|
|
|
user = FactoryGirl.create(:user)
|
|
CurrentUser.user = user
|
|
CurrentUser.ip_addr = "127.0.0.1"
|
|
MEMCACHE.flush_all
|
|
|
|
@post = FactoryGirl.create(:post)
|
|
end
|
|
|
|
context "Voting for a post" do
|
|
should "interpret up as +1 score" do
|
|
vote = PostVote.create(:post_id => @post.id, :vote => "up")
|
|
assert_equal(1, vote.score)
|
|
end
|
|
|
|
should "interpret down as -1 score" do
|
|
vote = PostVote.create(:post_id => @post.id, :vote => "down")
|
|
assert_equal(-1, vote.score)
|
|
end
|
|
|
|
should "not accept any other scores" do
|
|
vote = PostVote.create(:post_id => @post.id, :vote => "xxx")
|
|
assert(vote.errors.any?)
|
|
end
|
|
end
|
|
end
|