Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
What's the best way to spec the scheduler? #42
When defining the tasks to run I really want to spec them to ensure the tasks were actually scheduled correctly.
I have the following in mind, but not sure what's the best way to do it.
class TickTack attr_reader :scheduler def initialize @scheduler = Rufus::PlainScheduler.new # Note that I don't want to actually start it! scheduler.cron '0 22 * * 1-5' do # every day of the week at 22:00 (10pm) Security.activate end end def start! scheduler.start end end # Now I want to spec it similarly to: describe TickTack do def tick subject.scheduler.step #???? end it "should activate alarm on the weekdays after 10pm" do Timecop.travel Chronic.parse("next monday 22:01") Security.should_receive(:activate) tick end it "should not activate alarm on the weekdays before 10pm" do Timecop.travel Chronic.parse("next monday 8pm") Security.should_not_receive(:activate) tick end it "should not activate alarm on the weekend after 10pm" do Timecop.travel Chronic.parse("next sunday 22:01") Security.should_not_receive(:activate) tick end end
But I don't know if that's a a good way of doing it.
don't fall into the trap of testing rufus-scheduler. You're supposed to test your code.
You have to start the scheduler to make it check for jobs to trigger, else it's inert.
One way to test without starting the scheduler would be to make sure the jobs are queried with the right info. See Scheduler#jobs or #cron_jobs.
This comment has been minimized.
This comment has been minimized.Show comment Hide comment
Well, scheduling the jobs is the code I write (thus is mine) and do want to test it.
Checking the right jobs might be fine, but I'd rather want to make sure that the blocks are called at the correct times.
But maybe it's just not worth the effort...
Like I said above, you can query the scheduler via #jobs or #cron_jobs and check that the jobs in there hold the right information. That exactly matches your "that I provided correct information for the scheduler".