Rails task for automated testing preparation and execution

Ruby on Rails framework has great automated testing tools for unit, integration testing and others as well. this post will not discuss these tools, you should be familiar with them already.

I’m sharing here a task to help you prepare your test environment and run Rspec task without the need to run every command to run your test

Steps

Create your task file

Navigate to your lib\tasks folder, then create a test_db_setup.rake file

Put the code

Copy and paste the below code

</p>
<p>namespace 'app' do<br />
   desc &quot;Setup test environment database: drop, create, migrate, seed the test db and start you rspec tests&quot;<br />
task :test_db_setup =&gt; [:environment] do<br />
      Rails.env = ENV['RAILS_ENV'] = 'test'<br />
require 'rspec/core/rake_task'<br />
RSpec::Core::RakeTask.new(:spec)<br />
      Rake::Task['db:drop'].invoke<br />
      Rake::Task['db:create'].invoke<br />
      Rake::Task['db:migrate'].invoke<br />
      Rake::Task['db:seed'].invoke<br />
      puts &quot;Test environment is ready&quot;<br />
      puts &quot;Automated test will start ................................&quot;<br />
      Rake::Task['spec'].invoke<br />
      puts &quot;Finished ................................&quot;<br />
   end<br />
end</p>
<p>

Run your task

open your command line and navigate to your project source folder then run

</p>
<p>rake app:test_db_setup</p>
<p>

Share it with your mates 🙂

 

Donate-Button

Help to do more!

The content you read is available for free. If you’ve liked any of the articles at this site, please take a second to help us write more and more articles based on real experiences and maintain them for you and others. Your support will make it possible for us.

$5.00

Advertisements

Let me know your thoughts