
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
This gem provides helpers for integration testing with clockwork. Clockwork provides a cron-like utility for ruby. It works especially well in combination with timecop.
Image you have a rails app with a clockwork task scheduled every night to check for inactive users and sends them reminders via email.
Your clock.rb
file would look as follows:
module Clockwork
every(1.day, 'inactives', at: '00:00') do
User.inactive.each do
Mailer.reminder_mail.deliver_later
end
end
end
By using ClockworkMocks in combination with RSpec and Timecop, you could test this as follows:
RSpec.describe ... do
before { ClockworkMocks.reset_rspec(method(:allow), method(:receive)) }
let!(:user) { create(:user) }
context 'after 7 days without action' do
before do
Timecop.freeze 7.days.from_now
ClockworkMocks.work
end
after { Timecop.return }
it 'should have sent the user a reminder' do
expect(ActionMailer::Base.deliveries).not_to be_empty
end
end
end
Note that this does not replace proper unit tests, but gives you the possibility to additionally test your system as a whole.
Add this to your application's Gemfile:
group :test do
gem 'clockwork-mocks'
end
And then execute:
$ bundle
Or install it yourself as:
$ gem install clockwork-mocks
RSpec.describe ... do
before { ClockworkMocks.reset_rspec(method(:allow), method(:receive), 'path/to/clock.rb') }
end
If you do not pass a clock file path to ClockworkMocks.init_rspec
and you are in a rails environment it will assume "#{Rails.root}/clock.rb"
by default.
ClockworkMocks.scheduler.handler do |job, time|
# something
end
ClockworkMocks.scheduler.every(1.day, 'some task', at: '23:00') do
# something
end
Using this interface, you can use any stub provider to stub Clockwork
's methods and call ClockworkMocks.scheduler
's methods instead.
For example with rspec-mock:
allow(Clockwork).to receive(:handler) do |&block|
ClockworkMocks.scheduler.handler(&block)
end
allow(Clockwork).to receive(:every) do |interval, name, hash, &block|
ClockworkMocks.scheduler.every interval, name, hash, &block
end
load 'path/to/clock.rb'
At any time you can call ClockworkMocks.work
to execute all tasks that are due.
This works especially well in combination with timecop (although the latter is not a requirement):
Timecop.freeze(2.days.from_now) do
ClockworkMocks.work
end
Tasks will be executed in correct order. If enough time passed, tasks will be executed multiple times:
ClockworkMocks.scheduler.every(1.second, 'often') { puts 'often' }
ClockworkMocks.scheduler.every(2.seconds, 'not-so-often') { puts 'not so often' }
Timecop.freeze(3.seconds.from_now) do
ClockworkMocks.work
end
outputs
often
often
not so often
often
If you use clockwork to schedule sidekiq jobs but want them to actually execute during integration testing I recommend one of two options:
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/dpoetzsch/clockwork-mocks. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
Clockwork.handler
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the ClockworkMocks project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
FAQs
Unknown package
We found that clockwork-mocks demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.