This is pretty well covered in another SO question
My recommendation, repeated here in summary, is not to try testing rake tasks: extract the code to be tested to a class, model or lib function and test it there. Then just use rake to call the functions and manage dependencies.