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


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

Run your task

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


rake app:test_db_setup

Share it with your mates 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s