Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Rails using Minitest, how can I set up RuboCop to run automatically each time tests are run with rake?

When I run the following command, I want RuboCop to inspect application directories I specify before tests run:

bundle exec rake test
like image 846
klenwell Avatar asked Dec 01 '15 17:12

klenwell


People also ask

How to run test cases in Rails?

We can run all of our tests at once by using the bin/rails test command. Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case.

How do you run a Minitest in rails?

To run a Minitest test, the only setup you really need is to require the autorun file at the beginning of a test file: require 'minitest/autorun' . This is good if you'd like to keep the code small. A better way to get started with Minitest is to have Bundler create a template project for you.

How do I run Ruby file on Rubocop?

Enable/Disable RuboCop and Standard inspectionsGo to the Editor | Inspections page and enable/disable the RuboCop inspection under Ruby | Gems and gem management. If necessary, enable the Use 'standard' gem option to use the Standard wrapper.

Does Minitest work with rails?

These Minitest features are in the test runner introduced in Rails 5.0. There's no need for the database_cleaner gem anymore; Rails does the cleanup for us ('transactional tests'). Since Rails uses Selenium instead of the Capybara default, we don't need capybara-webkit and we don't need to do any extras to include testing of Javascript elements.

What is the best way to test Ruby programs?

If you use MiniTest, the second most popular choice for testing the Ruby written programs, there's a RuboCop extension dedicated to it, called RuboCop MiniTest. It enforces best practices outlined in the MiniTest Style Guide, yet another great read provided by RuboCop HeadQuarters.

What is Minitest in Ruby?

It’s small, fast, and it aims to make tests clean and readable. Minitest is the default testing suite used with Rails, so no further setup is required to get it to work. Along with RSpec, it is one of the two most commonly used testing suites used in Ruby.

How do I run all tests in rails?

$ rails test will run all the tests except the system tests. You need to explicitly run $ rails test:system. (Fun fact: the $ rails command will always run through bin/rails. No need to type $ bin/rails anymore.)


1 Answers

I added the following task to lib/tasks/test.rake:

require 'rubocop/rake_task'

# Add additional test suite definitions to the default test task here
namespace :test do
  desc 'Runs RuboCop on specified directories'
  RuboCop::RakeTask.new(:rubocop) do |task|
    # Dirs: app, lib, test
    task.patterns = ['app/**/*.rb', 'lib/**/*.rb', 'test/**/*.rb']

    # Make it easier to disable cops.
    task.options << "--display-cop-names"

    # Abort on failures (fix your code first)
    task.fail_on_error = false
  end
end

Rake::Task[:test].enhance ['test:rubocop']

The result:

$ bundle exec rake test
Running RuboCop...
Inspecting 9 files
.........

9 files inspected, no offenses detected
Run options: --seed 55148

# Running:

...................

Finished in 1.843280s, 10.3077 runs/s, 34.7207 assertions/s.

19 runs, 64 assertions, 0 failures, 0 errors, 0 skips
like image 139
klenwell Avatar answered Sep 24 '22 18:09

klenwell