Add specs for `RateLimiter::LimitExceeded#description`.

This commit is contained in:
Guo Xiang Tan 2018-06-19 07:48:03 +08:00
parent 3725fd8345
commit 630b4570ef
2 changed files with 30 additions and 10 deletions

View File

@ -10,16 +10,16 @@ class RateLimiter
end
def description
time_left = ""
if @available_in <= 3
time_left = I18n.t("rate_limiter.short_time")
elsif @available_in < 1.minute.to_i
time_left = I18n.t("rate_limiter.seconds", count: @available_in)
elsif @available_in < 1.hour.to_i
time_left = I18n.t("rate_limiter.minutes", count: (@available_in / 1.minute.to_i))
else
time_left = I18n.t("rate_limiter.hours", count: (@available_in / 1.hour.to_i))
end
time_left =
if @available_in <= 3
I18n.t("rate_limiter.short_time")
elsif @available_in < 1.minute.to_i
I18n.t("rate_limiter.seconds", count: @available_in)
elsif @available_in < 1.hour.to_i
I18n.t("rate_limiter.minutes", count: (@available_in / 1.minute.to_i))
else
I18n.t("rate_limiter.hours", count: (@available_in / 1.hour.to_i))
end
if @type.present?
type_key = @type.tr("-", "_")

View File

@ -0,0 +1,20 @@
require 'rails_helper'
RSpec.describe RateLimiter::LimitExceeded do
describe '#description' do
it 'should return the right description' do
[
[3, I18n.t("rate_limiter.short_time")],
[59, I18n.t("rate_limiter.seconds", count: 59)],
[3599, I18n.t("rate_limiter.minutes", count: 59)],
[7000, I18n.t("rate_limiter.hours", count: 1)]
].each do |available_in, time_left|
expect(described_class.new(available_in).description).to eq(I18n.t(
"rate_limiter.too_many_requests",
time_left: time_left
))
end
end
end
end