Running Rake tasks in Rspec Tests

xentek picture xentek · Aug 1, 2011 · Viewed 20.4k times · Source

I am building up an integration test suite and there is one bit of logic that I need to have a clean database for. How can I run the db:test:purge task inside of one of my tests?

I am using: ruby 1.9.2, rails 3.0.9, rspec 2.6

Answer

user290102 picture user290102 · Aug 1, 2011

You can invoke Rake tasks as following:

require 'rake'
Rake::Task[name].invoke

In this case this would result in the following code:

require 'rake'
Rake::Task['db:test:purge'].invoke