1# frozen_string_literal: true 2 3require 'fast_spec_helper' 4require_relative '../../../../rubocop/cop/code_reuse/worker' 5 6RSpec.describe RuboCop::Cop::CodeReuse::Worker do 7 subject(:cop) { described_class.new } 8 9 it 'flags the use of a worker in a controller' do 10 allow(cop) 11 .to receive(:in_controller?) 12 .and_return(true) 13 14 expect_offense(<<~SOURCE) 15 class FooController 16 def index 17 FooWorker.perform_async 18 ^^^^^^^^^^^^^^^^^^^^^^^ Workers can not be used in a controller. 19 end 20 end 21 SOURCE 22 end 23 24 it 'flags the use of a worker in an API' do 25 allow(cop) 26 .to receive(:in_api?) 27 .and_return(true) 28 29 expect_offense(<<~SOURCE) 30 class Foo < Grape::API::Instance 31 resource :projects do 32 get '/' do 33 FooWorker.perform_async 34 ^^^^^^^^^^^^^^^^^^^^^^^ Workers can not be used in a Grape API. 35 end 36 end 37 end 38 SOURCE 39 end 40 41 it 'flags the use of a worker in a Finder' do 42 allow(cop) 43 .to receive(:in_finder?) 44 .and_return(true) 45 46 expect_offense(<<~SOURCE) 47 class FooFinder 48 def execute 49 FooWorker.perform_async 50 ^^^^^^^^^^^^^^^^^^^^^^^ Workers can not be used in a Finder. 51 end 52 end 53 SOURCE 54 end 55 56 it 'flags the use of a worker in a Presenter' do 57 allow(cop) 58 .to receive(:in_presenter?) 59 .and_return(true) 60 61 expect_offense(<<~SOURCE) 62 class FooPresenter 63 def execute 64 FooWorker.perform_async 65 ^^^^^^^^^^^^^^^^^^^^^^^ Workers can not be used in a Presenter. 66 end 67 end 68 SOURCE 69 end 70 71 it 'flags the use of a worker in a Serializer' do 72 allow(cop) 73 .to receive(:in_serializer?) 74 .and_return(true) 75 76 expect_offense(<<~SOURCE) 77 class FooSerializer 78 def execute 79 FooWorker.perform_async 80 ^^^^^^^^^^^^^^^^^^^^^^^ Workers can not be used in a Serializer. 81 end 82 end 83 SOURCE 84 end 85 86 it 'flags the use of a worker in a model class method' do 87 allow(cop) 88 .to receive(:in_model?) 89 .and_return(true) 90 91 expect_offense(<<~SOURCE) 92 class User < ActiveRecord::Base 93 def self.execute 94 FooWorker.perform_async 95 ^^^^^^^^^^^^^^^^^^^^^^^ Workers can not be used in model class methods. 96 end 97 end 98 SOURCE 99 end 100end 101