 <p>You share methods in a module, and you place such a module inside the <code>lib</code> folder.</p> <p>Something like <code>lib/fake_data.rb</code> containing</p> <pre><code>module FakeData def random_address [Faker::Address.street_address, Faker::Address.city].join("\n") end module_function end </code></pre> <p>and inside your rake task just require the module, and call <code>FakeData.random_address</code>.</p> <p>But, if it is like a seed you need to do every time you run your tests, you should consider adding this to your general <code>before all</code>.</p> <p>E.g. my <code>spec_helper</code> looks like this:</p> <pre><code># Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } RSpec.configure do |config| config.use_transactional_fixtures = true config.infer_base_class_for_anonymous_controllers = false config.order = "random" include SetupSupport config.before(:all) do load_db_seed end end </code></pre> <p>and the module <code>SetupSupport</code> is defined in <code>spec/support/setup_support.rb</code> and looks as follows:</p> <pre><code>module SetupSupport def load_db_seed load(File.join(Rails.root, 'db', 'seeds.rb')) end end </code></pre> <p>Not sure if you need to load the seeds, or are already doing this, but this is the ideal spot to also generate needed fake data.</p> <p>Note that my setup support class is defined in <code>spec/support</code> because the code is only relevant to my specs, I have no rake task also needing the same code.</p>