你将如何测试这门课?您会将它与 Resque 集成并检查作业是否已放入队列中,还是要模拟它,如果要模拟它,您将如何避免像示例规范中所示那样重复代码。
class BookingReminderEnqueuer
def initialize(user, booking, worker = BookingReminder)
@user, @booking, @worker = user, booking, worker
end
def enqueue
Resque.enqueue_at(
remind_user_at,
@worker,
booking_id: @booking.id,
user_id: @user.id
) if @booking.remind_user?
end
private
def remind_user_at
@booking.start_time - 6.hours
end
end
require 'spec_helper'
describe BookingReminderEnqueuer
describe "#enqueue" do
context "when the user should have a reminder" do
it "enqueues the reminder" do
booking.stub(:remind_user?) { true }
Resque.should_receive(:enqueue_at).with(
booking.start_time - 6.hours,
BookingReminder,
booking_id: booking.id,
user_id: user.id
).once
booking_reminder_enqueuer.enqueue
end
end
context "when the user shouldn't have a reminder" do
it "does not enqueue the reminder" do
booking.stub(:remind_user?) { false }
Resque.should_not_receive(:enqueue_at)
end
end
end
end