Select Git revision
community_user.rb
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
badge_great_question_test.rb 1.70 KiB
require 'test_helper'
class BadgeGreatQuestionTest < ActionController::TestCase
BRONZE_VOTES = 3
SILVER_VOTES = 5
GOLD_VOTES = 7
def setup
@post = posts(:question_no_up_votes)
@user = @post.user
@badge = Badge.find_by!(id: BadgeGreatQuestion.badge_id)
end
test 'user receives correct badge type when post reaches score thresholds' do
create_votes(1, BRONZE_VOTES, @post)
check_score_and_badge_type(BadgeGreatQuestion.badge_award_thresholds[0], 'bronze')
create_votes(BRONZE_VOTES + 1, SILVER_VOTES, @post)
check_score_and_badge_type(BadgeGreatQuestion.badge_award_thresholds[1], 'silver')
create_votes(SILVER_VOTES + 1, GOLD_VOTES, @post)
check_score_and_badge_type(BadgeGreatQuestion.badge_award_thresholds[2], 'gold')
end
test 'user does not receive badge when post does not reach votes threshold' do
create_votes(1, BRONZE_VOTES - 1, @post)
assert_equal @post.score < BadgeGreatQuestion.badge_award_thresholds[0], true
assert_equal @user.user_badges.where(badge_id: @badge.id, badge_source: @post).count, 0
end
def create_votes(start, stop, post)
(start..stop).each do |i|
unique_user = users("user_test_#{i}")
vote_attributes = votes(:one).attributes
vote_attributes.delete('id')
vote_attributes['user_id'] = unique_user.id
vote_attributes['post_id'] = post.id
post.votes.create!(vote_attributes)
end
end
def check_score_and_badge_type(score, badge_type)
assert_equal @post.score >= score, true
assert_equal @user.user_badges.where(badge_id: @badge.id, badge_source: @post).count, 1
assert_equal @user.user_badges.where(badge_id: @badge.id, badge_source: @post).first.badge_type, badge_type
end
end