Skip to content

Instantly share code, notes, and snippets.

@jerrygreen
Last active August 1, 2017 06:50
Show Gist options
  • Save jerrygreen/cab79e62f4d8e08eabc19cc8a2776046 to your computer and use it in GitHub Desktop.
Save jerrygreen/cab79e62f4d8e08eabc19cc8a2776046 to your computer and use it in GitHub Desktop.
Rails QA automation

Setup Rails QA automation to your project

1. Add rubocop to you project

Look for last version or use this:

gem 'rubocop', '0.47.1', require: false

And add simple default-like rubocop configuration (run this in your project folder):

curl https://gist.githubusercontent.com/JerryGreen/cab79e62f4d8e08eabc19cc8a2776046/raw/rubocop.yml > .rubocop.yml

TODO: maybe use this https://github.com/toshimaru/rubocop-rails ?

2. Start using Minitest, add FactoryGirl, SimpleCov, Minitest::Reporters

  1. Minitest is the main thing here. Lets you writing rails tests.
  2. FactoryGirl is your real friend helping you creating objects. Read this guide.
  3. SimpleCov is just 2 lines in test_helper.rb that generates coverage statistics for your project.
  4. Minitest::Reporters is just another 2 lines in test_helper.rb that makes output of test runs much prettier.

Minitest already is a part of Rails so just add this:

group :development, :test do
  gem 'factory_girl_rails', '4.8.0'
end

group :test do
  gem 'minitest-reporters', '1.1.14'
  gem 'simplecov', '0.14.1', require: false
end

And expand your test_helper.rb to get it all work:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'simplecov'
SimpleCov.start 'rails'
require "minitest/reporters"
Minitest::Reporters.use!
require 'rails/test_help'

module ActiveSupport
  class TestCase
    include FactoryGirl::Syntax::Methods
  end
end

I highly recommend to change default strategy of FactoryGirl to build strategy, so add this lines to end of test.rb and development.rb environments too:

module FactoryGirl
  class Factory
    def default_strategy
      @options[:default_strategy] || :build
    end
  end
end

P.S. I recommend writing controller tests as they are the most helpful.

3. Install scripts into your project to automate test&lint runs

Run this in your project folder:

curl https://gist.githubusercontent.com/JerryGreen/cab79e62f4d8e08eabc19cc8a2776046/raw/pre-commit.sh > .pre-commit.sh
curl -O https://gist.githubusercontent.com/JerryGreen/cab79e62f4d8e08eabc19cc8a2776046/raw/install-pre-commit.sh
chmod +x .pre-commit.sh
chmod +x install-pre-commit.sh
./install-pre-commit.sh

Now you can commit your changes. And after:

4. Say to your teammates to install pre-commit script

You should also add this command to your readme file

./install-pre-commit.sh

5. Share this info with your team

Just copy&paste this list so they know how to use it right:

More info

Existing .pre-commit.sh should be enough but feel free to change this file, if you want to improve/change the automate process. Your teammates don't need to do somethings else as they have a symlink to this file.

#!/bin/bash
git config --unset core.hooksPath
ln -s -f ../../.pre-commit.sh .git/hooks/pre-commit
#!/bin/bash
errors=()
# Forbid to push to master
BRANCH=`git rev-parse --abbrev-ref HEAD`
if [ $BRANCH == master ]; then
errors+=("[PREVENT] --> ✋ Non shall commit to master!")
fi
# Run linter
rubocop || errors+=("[RUBOCOP] --> ✋ Your code is disgusting!")
rubocop Gemfile || errors+=("[RUBOCOP] --> ✋ Your Gemfile is messy!")
# Run tests
rails test || errors+=("[TESTING] --> ✋ Your code is not working!")
# Show results
if [ ${#errors[@]} -eq 0 ]; then
echo "[OK] --> No errors, hooray"
exit 0
else
printf '%s\n' "${errors[@]}"
exit 1
fi
Rails:
Enabled: true
Documentation:
Enabled: false
AllCops:
TargetRubyVersion: 2.4
Include:
- Rakefile
- config.ru
- lib/**/*.rake
- '**/Gemfile'
Exclude:
- db/schema.rb
Metrics/LineLength:
Max: 120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment