I am trying to invoke a rake task in in my rspec.
require "rake"
rake = Rake::Application.new
Rake.application = rake
rake.init
rake.load_rakefile
rake['rake my:task'].invoke
But i am getting error
Failure/Error: rake['rake db:migrate'].invoke
RuntimeError:
Don't know how to build task 'rake db:migrate'
Does anyone have a idea how we can invoke rake task in rspec code.
Any help would be highly appreciated.
To pass in the arguments in square brackets to
invoke
:becomes:
If your
args
are in an array, you can do the following:More info at this StackOverflow question: How to run Rake tasks from within Rake tasks?.
Small namespacing issue, the task is
db:migrate
notrake db:migrate
like the command line usage.So changing it to this should help:
A simpler solution for Rails with Rspec :
In your
spec_helper
(orrails_helper
for newer versions of rspec-rails) :Then when you want to invoke your task you can do the following :