Grace à cet article (et ceux cités dedans !), je peux tester une tâche rake avec Rspec.
Cependant j’avais besoin de passer des paramètres à ma tâche rake.
Ceci peut être fait en utilisant la variable ENV :
describe "app rake tasks" do
before :each do
@rake = Rake::Application.new
Rake.application = @rake
load "#{RAILS_ROOT}/lib/tasks/read_file.rake"
@file_path = "#{RAILS_ROOT}/tmp/jaccuse.txt"
ENV["file_to_read"] = @file_path # paramètre passé à la tâche rake
end
Dans la tâche rake (lib/tasks/read_file.rake) :
task :validate => :environment do
puts File.read(ENV["file_to_read"])
end
En fait, ça équivaut à lancer la tâche :
rake read_file file_to_read=jaccuse.txt