2019-10-29 12:06:40 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-17 06:08:43 +00:00
|
|
|
require 'fast_spec_helper'
|
2018-06-18 08:31:41 +00:00
|
|
|
|
|
|
|
require_relative '../../../../rubocop/cop/gitlab/finder_with_find_by'
|
|
|
|
|
2021-01-07 18:10:38 +00:00
|
|
|
RSpec.describe RuboCop::Cop::Gitlab::FinderWithFindBy do
|
2018-06-18 08:31:41 +00:00
|
|
|
subject(:cop) { described_class.new }
|
|
|
|
|
|
|
|
context 'when calling execute.find' do
|
2021-01-26 18:09:30 +00:00
|
|
|
it 'registers an offense and corrects' do
|
|
|
|
expect_offense(<<~CODE)
|
|
|
|
DummyFinder.new(some_args)
|
|
|
|
.execute
|
|
|
|
.find_by!(1)
|
|
|
|
^^^^^^^^ Don't chain finders `#execute` method with [...]
|
|
|
|
CODE
|
|
|
|
|
|
|
|
expect_correction(<<~CODE)
|
|
|
|
DummyFinder.new(some_args)
|
|
|
|
.find_by!(1)
|
|
|
|
CODE
|
2018-06-18 08:31:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when called within the `FinderMethods` module' do
|
2021-01-26 18:09:30 +00:00
|
|
|
it 'does not register an offense' do
|
|
|
|
expect_no_offenses(<<~SRC)
|
2018-06-18 08:31:41 +00:00
|
|
|
module FinderMethods
|
|
|
|
def find_by!(*args)
|
|
|
|
execute.find_by!(args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
SRC
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|