How to check if rake task was finished?

岁酱吖の 提交于 2021-01-28 09:24:11

问题


This is my first experience with RoR and I using the rspec tool to test my controller. But inside the methods, I using rake tasks to create asynchronous solutions like this:

 def scan_all_urls
   system 'rake scan_all_urls &'
 end

And my rake task is this:

 task :scan_all_urls => :environment do
   crawler = Crawler.new
   urls = Url.all
   crawler.scan_urls(urls)
 end

But my issue is, at the rspec test:

it 'should scan all url' do
    params = {}
    get 'scan_all_urls', params: params, format: :json
end

How I could set to wait until the rake task finish?


回答1:


You can't really check that rake task finished. I would suggest to you write good specs for Crawler#scan_urls and maybe check in the controller that Crawler was called.

Also, test returning values in your controller spec. So the controller spec might look like this

it 'should scan all url' do
    expect(Crawler).to receive(:new).and_call_original

    params = {}
    get 'scan_all_urls', params: params, format: :json

    expect(MultiJson.load response.body).to # something
end  


来源:https://stackoverflow.com/questions/47140835/how-to-check-if-rake-task-was-finished

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!