Refactoring: Add possibility to write tests for custom Zammad rubocop cops.

This commit is contained in:
Thorsten Eckel 2022-01-31 15:10:31 +01:00 committed by Martin Gruner
parent 8dd045af75
commit f79e8c72cd
2 changed files with 53 additions and 0 deletions

View file

@ -0,0 +1,24 @@
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
require 'rails_helper'
require_relative '../../../../.rubocop/cop/zammad/have_no_over_not_to'
RSpec.describe RuboCop::Cop::Zammad::HaveNoOverNotTo, type: :rubocop do
it 'accepts .to have_no' do
expect_no_offenses("expect(page).to have_no_css('#elem')")
end
context 'with .not_to have' do
it 'registers an offense' do
expect_offense(<<~RUBY)
expect(page).not_to have_css('#elem')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `.to have_no_css` over `.not_to have_css`.
RUBY
expect_correction(<<~RUBY)
expect(page).to have_no_css('#elem')
RUBY
end
end
end

29
spec/support/rubocop.rb Normal file
View file

@ -0,0 +1,29 @@
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
require 'rubocop/rspec/support'
# https://github.com/rubocop/rubocop/tree/91e72f8bb4a5a646845e7915052f912d60a3d280/lib/rubocop/rspec/shared_contexts.rb:52
RSpec.shared_context 'when checking custom RuboCop cops' do
include RuboCop::RSpec::ExpectOffense
let(:cop_options) { {} }
let(:cop_config) { {} }
let(:cur_cop_config) do
RuboCop::ConfigLoader
.default_configuration.for_cop(described_class)
.merge({
'Enabled' => true, # in case it is 'pending'
'AutoCorrect' => true # in case defaults set it to false
})
.merge(cop_config)
end
let(:config) { ::RuboCop::Config.new({ described_class.cop_name => cur_cop_config }, "#{Rails.configuration.root}/.rubocop.yml") }
let(:cop) { described_class.new(config, cop_options) }
end
RSpec.configure do |config|
config.include_context 'when checking custom RuboCop cops', type: :rubocop
end