问题
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