-
Notifications
You must be signed in to change notification settings - Fork 2
Testing
Daniel Pepper edited this page Aug 12, 2024
·
6 revisions
Berater has a few tools to make testing easier and plays nicely with Timecop.
Force all .limit
calls to either pass or fail, without hitting Redis. Berater itself is extensively tested to ensure functionality - you're welcome to retest the library, but by and large your test suite should bypass the internals and explicitly test pass/fail behavior using Berater.test_mode
. This will improve test performance and stability.
require 'berater/test_mode'
describe 'MyTest' do
let(:limiter) { Berater.new(:key, 1, :second) }
context 'with test_mode = :pass' do
before { Berater.test_mode = :pass }
it 'always works' do
10.times { limiter.limit { ... } }
end
end
context 'with test_mode = :fail' do
before { Berater.test_mode = :fail }
it 'always raises an exception' do
expect { limiter.limit }.to raise_error(Berater::Overloaded)
end
end
end
rspec matchers and automatic flushing of Redis between examples.
require 'berater/rspec'
describe 'MyTest' do
let(:limiter) { Berater.new(:key, 1, :second) }
it 'rate limits' do
limiter.limit
expect { limiter.limit }.to be_overloaded
end
end
Recommended setup in spec/spec_helper.rb
:
require 'berater/rspec'
RSpec.configure do |config|
config.before { Berater.test_mode = :pass }
end