Rails - Rake test and rubocop in one task
You could easily define your own rake task which first invokes Rails' test
rake task and then the code snippet you mentioned for rubocop.
For example, in a .rake file you could have something like that:
require 'rubocop/rake_task'
desc 'Run tests and rubocop'
task :my_test do
Rake::Task['test'].invoke
RuboCop::RakeTask.new
end
If you feel the need to customize the call to Rubocop and that involves more code, you could create another custom task, say :rubocop, which you then invoke from :my_test as well.
Finally, an alternative to creating your own rake task and sticking with rake test
would be to modify your test_helper to invoke whatever you need invoked after testing is completed.
With Rails using Minitest, how can I set up RuboCop to run automatically each time tests are run with rake?
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
Keep getting an Error while running rubocop with Travis CI
Look a little closer at the error:
Error: obsolete parameter AlignWith (for Lint/EndAlignment) found in /home/travis/build/firehosefirechess/chess_app/vendor/bundle/ruby/2.4.0/gems/rake-12.0.0/.rubocop.yml AlignWith has been renamed to EnforcedStyleAlignWith
It's not your app's .rubocop.yml where the error is showing up, it's actually in chess_app/vendor/bundle/ruby/2.4.0/gems/rake-12.0.0/.rubocop.yml
!
As you probably don't want to be running Rubocop on vendor code, you can resolve this by adding a line to the Exclude
list in your own config:
- 'vendor/**/*'
RSpec & Rubocop / Ruby Style Guide
I would recommend making a pull request to RSpec changing their templates to be Rubocop-compliant.
Related Topics
Rspec: Testing Assignment of Instance Variable
How to Fix Libv8 Error from Gemfile on Mavericks
Implementing Bayesian Classifier in Ruby
"Gem Update --System Is Disabled on Debian" Error
How to Embed Dynamic Ruby Code to "Javascript" Section in Slim Templates
How to Deal with Ruby 2.1.2 Memory Leaks
Devise Nomethoderror 'For' Parametersanitizer
How to Find Best Matching Element in Array of Numbers
Installing MySQL2 Gem for Ruby on Rails 3.1.0
Rails First_Or_Create Activerecord Method
Ruby W/ Sinatra: What Is the Equivalent of a .Js.Erb from Rails
How to Modify the Input Type of the Rails Datetime_Select Helper
No Source for Ruby-2.0.0-P451 Provided with Debugger-Ruby_Core_Source Gem