Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Cop idea: Don’t use job.perform or job_class.new.perform, prefer perform_now to perform_later when testing jobs #12

Open
ydakuka opened this issue Sep 16, 2023 · 0 comments

Comments

@ydakuka
Copy link

ydakuka commented Sep 16, 2023

Reference:

https://github.com/toptal/active-job-style-guide#perform

https://github.com/toptal/active-job-style-guide#perform_later

# bad - `perform` method is called directly on an implicitly defined subject
RSpec.describe SomeJob do
  # implicitly defined `subject` is `SomeJob.new`
  it 'updates user status' do
    expect { subject.perform(user) }.to change { user.status }.to(:updated) }
  end
end

# bad - `perform` method is called directly on a job instance
RSpec.describe SomeJob do
  it 'updates user status' do
    expect { SomeJob.new.perform(user) }.to change { user.status }.to(:updated) }
  end
end

# good
RSpec.describe SomeJob do
  it 'updates user status' do
    expect { SomeJob.perform_now(user) }.to change { user.status }.to(:updated) }
  end
end
# bad - unnecessary roundtrip to Redis
RSpec.describe SomeJob do
  it 'updates user status' do
    expect do
      SomeJob.perform_later(user)
      perform_scheduled_jobs
    end.to change { user.status }.to(:updated) }
  end
end

# good
RSpec.describe SomeJob do
  it 'updates user status' do
    expect { SomeJob.perform_now(user) }.to change { user.status }.to(:updated) }
  end
end
@ydakuka ydakuka changed the title Cop idea: Don’t use job.perform or job_class.new.perform in specs Cop idea: Don’t use job.perform or job_class.new.perform, prefer perform_now to perform_later when testing jobs Sep 16, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant