Skip to content

Instantly share code, notes, and snippets.

@michaelbarton
Created May 29, 2009 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelbarton/119890 to your computer and use it in GitHub Desktop.
Save michaelbarton/119890 to your computer and use it in GitHub Desktop.
Can I over-ride task :environment in test_helper.rb to test rake tasks?
I have a series of rake tasks in a Rakefile which I'd like to test as part of my specs etc. Each task is defined in the form:
task :do_somthing => :environment do
# Do something with the database here
end
Where the :environment task sets up an ActiveRecord/DataMapper database connection and classes. I'm not using this as part of Rails but I have a series of tests which I like to run as part of BDD.
This snippet illustrates how I'm trying to test the rake tasks, using this blog post [1] as an example.
def setup
@rake = Rake::Application.new
Rake.application = @rake
load File.dirname(__FILE__) + '/../../tasks/do_something.rake'
end
should "import data" do
@rake["do_something"].invoke
assert something_in_the_database
end
So my request for help - is it possible to over-ride the :environment task in my test_helper.rb file so I my rake testing interacts with the my test database, rather than production? I've tried redefining the task in the helper file, but this doesn't work.
This further blog post [2] has provided some inspiration, but I'm still not getting the desired behaviour. Any help for a solution would be great, as I've been stuck on this for the past week.
[1] http://blog.nicksieger.com/articles/2007/06/11/test-your-rake-tasks
[2] http://www.dcmanges.com/blog/modifying-rake-tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment