Ruby on rails 如何使用RSpec测试延迟的_作业回调挂钩
我想验证是否调用了延迟的_作业回调挂钩,但我不知道如何让RSpec这样做,特别是当涉及到几个类层时 假设我有这样一个ActiveRecord模型:Ruby on rails 如何使用RSpec测试延迟的_作业回调挂钩,ruby-on-rails,rspec,delayed-job,Ruby On Rails,Rspec,Delayed Job,我想验证是否调用了延迟的_作业回调挂钩,但我不知道如何让RSpec这样做,特别是当涉及到几个类层时 假设我有这样一个ActiveRecord模型: class MyJob < ActiveRecord::Base def perform # do some stuff end def after(job) # called by delayed_job after perform completes end end 我仔细研究了stackoverflow、
class MyJob < ActiveRecord::Base
def perform
# do some stuff
end
def after(job)
# called by delayed_job after perform completes
end
end
我仔细研究了stackoverflow、relishapp.com/rspec以及我能想到的所有其他地方。不会太难吧?(我打赌@zettetic在睡梦中知道答案…;)
(注意:我的实际案例在MyJob和DJ之间使用了一个“shim”类。这个简单案例的答案可能会引发更复杂的后续问题!)我的建议是查看Jobs表是否创建了新的job记录,然后反思该记录的详细信息。我以前也用过类似的东西
it "should sync content from the remote CMS" do
DelayedJobs::CMS::Sync.module_eval do
def perform
url.should == "http://example.com/tools/datafeed/resorts/all"
Report.sync!([{'id' => 1, 'name' => 'resort foo'}])
end
end
lambda do
Report.sync_all!
end.should change(Delayed::Job, :count)
lambda do
Delayed::Job.work_off
end.should change(Resort, :count).by(2)
end
我觉得这不是最好的办法 通过这样做,你试图规范DelayedJob的行为,而不是你的应用程序,而DJ已经有了自己的一套测试
不管怎样,嘲笑DelayedJob本身怎么样?也许你可以指定,通过这样做,你的MockedJob将在处理后调用你的钩子,并且你可以相应地创建你的匹配器/期望…如果你只想在调用后验证
,你能不能删除。(一个…
需求的实例?)作为旁注,我倾向于使用Delayed::Worker.new(:quiet=>true)。努力让工作人员和任何抱怨保持沉默。不过,我承认,可能会有一个例外,因为这个原因我没有看到。远离这个问题已经两年多了(!)我认为你是对的:这个问题,如前所述,只是试图验证DJ的行为,而不是验证我的代码。
it "should sync content from the remote CMS" do
DelayedJobs::CMS::Sync.module_eval do
def perform
url.should == "http://example.com/tools/datafeed/resorts/all"
Report.sync!([{'id' => 1, 'name' => 'resort foo'}])
end
end
lambda do
Report.sync_all!
end.should change(Delayed::Job, :count)
lambda do
Delayed::Job.work_off
end.should change(Resort, :count).by(2)
end