Challenge 3: Testing the Job — Solution # spec/jobs/resize_image_job_spec.rb RSpec.describe ResizeImageJob, type: :job do it "enqueues the job with the correct image_id" do image = create(:image) expect { ResizeImageJob.perform_later(image.id) } .to have_enqueued_job.with(image.id) end it "calls resize! on the image when performed" do image = create(:image) expect(image).to receive(:resize!) allow(Image).to receive(:find).with(image.id).and_return(image) ResizeImageJob.perform_now(image.id) end end =begin Notes: - The first example checks only that enqueuing happened with the right argument -- have_enqueued_job.with(image.id) -- without running perform at all, matching the chapter's "was it enqueued" vs "does it work when it runs" split. - The second example stubs Image.find to return the exact in-memory image instance (so the expectation on that specific object can be set), then sets expect(image).to receive(:resize!) before calling perform_now -- RSpec's message expectation confirms resize! was actually called during the job's execution, not just that no error was raised. - perform_now runs the job's perform method synchronously and immediately, which is what lets this example assert on resize! being called within the same test, rather than needing an async worker process to actually execute it. =end