|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::RSpecRails::ReceivePerformLater do |
| 4 | + context 'when using expect with receive(:perform_later)' do |
| 5 | + it 'registers an offense' do |
| 6 | + expect_offense(<<~RUBY) |
| 7 | + it 'enqueues a job' do |
| 8 | + expect(MyJob).to receive(:perform_later) |
| 9 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `expect { ... }.to have_enqueued_job(MyJob)` over `expect(MyJob).to receive(:perform_later)`. |
| 10 | + do_something |
| 11 | + end |
| 12 | + RUBY |
| 13 | + end |
| 14 | + |
| 15 | + it 'registers an offense with not_to' do |
| 16 | + expect_offense(<<~RUBY) |
| 17 | + it 'does not enqueue a job' do |
| 18 | + expect(MyJob).not_to receive(:perform_later) |
| 19 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `expect { ... }.to have_enqueued_job(MyJob)` over `expect(MyJob).not_to receive(:perform_later)`. |
| 20 | + do_something |
| 21 | + end |
| 22 | + RUBY |
| 23 | + end |
| 24 | + |
| 25 | + it 'registers an offense with to_not' do |
| 26 | + expect_offense(<<~RUBY) |
| 27 | + it 'does not enqueue a job' do |
| 28 | + expect(MyJob).to_not receive(:perform_later) |
| 29 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `expect { ... }.to have_enqueued_job(MyJob)` over `expect(MyJob).to_not receive(:perform_later)`. |
| 30 | + do_something |
| 31 | + end |
| 32 | + RUBY |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + context 'when using expect with receive(:perform_later).with()' do |
| 37 | + it 'registers an offense for receive with arguments' do |
| 38 | + expect_offense(<<~RUBY) |
| 39 | + it 'enqueues a job with arguments' do |
| 40 | + expect(MyJob).to receive(:perform_later).with(user, order) |
| 41 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `expect { ... }.to have_enqueued_job(MyJob)` over `expect(MyJob).to receive(:perform_later)`. |
| 42 | + do_something |
| 43 | + end |
| 44 | + RUBY |
| 45 | + end |
| 46 | + |
| 47 | + it 'registers an offense for receive with keyword arguments' do |
| 48 | + expect_offense(<<~RUBY) |
| 49 | + it 'enqueues a job with keyword arguments' do |
| 50 | + expect(MyJob).to receive(:perform_later).with(user_id: 1) |
| 51 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `expect { ... }.to have_enqueued_job(MyJob)` over `expect(MyJob).to receive(:perform_later)`. |
| 52 | + do_something |
| 53 | + end |
| 54 | + RUBY |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + context 'when using allow with have_received(:perform_later)' do |
| 59 | + it 'registers an offense' do |
| 60 | + expect_offense(<<~RUBY) |
| 61 | + it 'enqueues a job' do |
| 62 | + allow(MyJob).to receive(:perform_later) |
| 63 | + do_something |
| 64 | + expect(MyJob).to have_received(:perform_later) |
| 65 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `expect { ... }.to have_enqueued_job(MyJob)` over `expect(MyJob).to have_received(:perform_later)`. |
| 66 | + end |
| 67 | + RUBY |
| 68 | + end |
| 69 | + |
| 70 | + it 'registers an offense with arguments' do |
| 71 | + expect_offense(<<~RUBY) |
| 72 | + it 'enqueues a job with arguments' do |
| 73 | + allow(MyJob).to receive(:perform_later) |
| 74 | + do_something |
| 75 | + expect(MyJob).to have_received(:perform_later).with(user) |
| 76 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `expect { ... }.to have_enqueued_job(MyJob)` over `expect(MyJob).to have_received(:perform_later)`. |
| 77 | + end |
| 78 | + RUBY |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + context 'when using allow with receive(:perform_later)' do |
| 83 | + it 'does not register an offense for allow alone' do |
| 84 | + expect_no_offenses(<<~RUBY) |
| 85 | + it 'allows job enqueuing' do |
| 86 | + allow(MyJob).to receive(:perform_later) |
| 87 | + do_something |
| 88 | + end |
| 89 | + RUBY |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + context 'when using have_enqueued_job matcher' do |
| 94 | + it 'does not register an offense' do |
| 95 | + expect_no_offenses(<<~RUBY) |
| 96 | + it 'enqueues a job' do |
| 97 | + expect { do_something }.to have_enqueued_job(MyJob) |
| 98 | + end |
| 99 | + RUBY |
| 100 | + end |
| 101 | + |
| 102 | + it 'does not register an offense with arguments' do |
| 103 | + expect_no_offenses(<<~RUBY) |
| 104 | + it 'enqueues a job with arguments' do |
| 105 | + expect { do_something }.to have_enqueued_job(MyJob).with(user, order) |
| 106 | + end |
| 107 | + RUBY |
| 108 | + end |
| 109 | + |
| 110 | + it 'does not register an offense with queue and timing' do |
| 111 | + expect_no_offenses(<<~RUBY) |
| 112 | + it 'enqueues a job with options' do |
| 113 | + expect { do_something } |
| 114 | + .to have_enqueued_job(MyJob) |
| 115 | + .on_queue('mailers') |
| 116 | + .at(Date.tomorrow.noon) |
| 117 | + end |
| 118 | + RUBY |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + context 'when using receive with other methods' do |
| 123 | + it 'does not register an offense for receive(:perform_now)' do |
| 124 | + expect_no_offenses(<<~RUBY) |
| 125 | + it 'performs a job' do |
| 126 | + expect(MyJob).to receive(:perform_now) |
| 127 | + do_something |
| 128 | + end |
| 129 | + RUBY |
| 130 | + end |
| 131 | + |
| 132 | + it 'does not register an offense for receive(:some_method)' do |
| 133 | + expect_no_offenses(<<~RUBY) |
| 134 | + it 'calls some method' do |
| 135 | + expect(MyJob).to receive(:some_method) |
| 136 | + do_something |
| 137 | + end |
| 138 | + RUBY |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + context 'when using receive on non-job objects' do |
| 143 | + it 'does not register an offense for instance methods' do |
| 144 | + expect_no_offenses(<<~RUBY) |
| 145 | + it 'receives a method' do |
| 146 | + expect(instance).to receive(:perform_later) |
| 147 | + instance.perform_later |
| 148 | + end |
| 149 | + RUBY |
| 150 | + end |
| 151 | + end |
| 152 | +end |
0 commit comments