我在作业控制器中有一个方法可以强制某些延迟作业立即运行
def retry_queued
@jobs = Delayed::Job.awaiting_retry.update_all(run_at: Time.zone.now)
respond_to do |format|
format.html { redirect_to jobs_path, notice: "Queued jobs getting retried." }
format.json render json: { status: 'success' }
end
end
使用范围。
我正在尝试使用rspec及其在请求规范中的给定请求方式来测试这个
describe "POST retry_queued" do
before do
@not_running = Delayed::Job.create(run_at: Time.zone.now - 1.hour)
@not_running.update_attribute(:attempts, 1)
@from = Delayed::Job.last.run_at
sign_in users(:jane)
end
it "run the queued job" do
expect(Delayed::Job.last.run_at.to_s).not_to eq(Time.zone.now.to_s)
post :retry_queued
expect(Delayed::Job.last.run_at.to_s).to eq(Time.zone.now.to_s)
end
end
我的路线片段
resources :jobs, :only => [:index, :destroy] do
member do
put :run
end
collection do
delete :destroy_failed
delete :destroy_all
post :retry_queued
end
end
rspec错误低于
1) JobsController POST retry_queued run the queued job
Failure/Error: post :retry_queued
ArgumentError:
wrong number of arguments (given 2, expected 1)
和我的link_to点击它
<%= link_to retry_queued_jobs_path, class: "btn btn-default", method: :post do %>
<span class="glyphicon glyphicon-refresh"></span> Retry queued jobs
<% end %>
好像它要求我把论据传递给帖子,但我没有。我只是希望我的方法受到打击。我记得我可以发帖而不必在过去指定params但是不记得我做了什么让我得到了错误。
目前使用rspec 3.2
答案 0 :(得分:0)
在大多数情况下,根据时间测试某些东西不是一个好主意,因为时间测量的精确度超过了计算速度,特别是在处理相当复杂的操作(例如重新排列作业)时。我建议您尝试timecop
gem并查看结果。
答案 1 :(得分:0)
答案是我正在渲染json不正确它应该是format.json { head :no_content }
而不是format.json render json: { status: 'success' }