From 56358fe6adaad15a874e8ed8b7de210630c610e9 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 21 Dec 2016 20:41:33 -0600 Subject: [PATCH] Add List The Enqueued Jobs as a rails til --- README.md | 3 ++- rails/list-the-enqueued-jobs.md | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 rails/list-the-enqueued-jobs.md diff --git a/README.md b/README.md index fc16d58..06bcd0e 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really warrant a full blog post. These are mostly things I learn by pairing with smart people at [Hashrocket](http://hashrocket.com/). -_491 TILs and counting..._ +_492 TILs and counting..._ --- @@ -316,6 +316,7 @@ _491 TILs and counting..._ - [Demodulize A Class Name](rails/demodulize-a-class-name.md) - [Hash Slicing](rails/hash-slicing.md) - [Ignore Poltergeist JavaScript Errors](rails/ignore-poltergeist-javascript-errors.md) +- [List The Enqueued Jobs](rails/list-the-enqueued-jobs.md) - [Migrating Up Down Up](rails/migrating-up-down-up.md) - [Params Includes Submission Button Info](rails/params-includes-submission-button-info.md) - [Perform SQL Explain With ActiveRecord](rails/perform-sql-explain-with-activerecord.md) diff --git a/rails/list-the-enqueued-jobs.md b/rails/list-the-enqueued-jobs.md new file mode 100644 index 0000000..86cd242 --- /dev/null +++ b/rails/list-the-enqueued-jobs.md @@ -0,0 +1,33 @@ +# List The Enqueued Jobs + +Many Rails apps need to delegate work to jobs that can be performed at a +later time. Both unit and integration testing can benefit from asserting +about the jobs that get enqueued as part of certain methods and workflows. +Rails provides a handy helper method for checking out the set of enqueued +jobs at any given time. + +The +[`enqueued_jobs`](http://api.rubyonrails.org/classes/ActiveJob/QueueAdapters/TestAdapter.html#method-i-enqueued_jobs) +method will provide a store of all the currently enqueued jobs. + +It provides a number of pieces of information about each job. One way to +use the information is like so: + +```ruby +describe '#do_thing' do + it 'enqueues a job to do a thing later' do + Processor.do_thing(arg1, arg2) + expect(enqueued_jobs.map { |job| job[:job] }).to match_array([ + LongProcessJob, + SendEmailsJob + ]) + end +end +``` + +To use this in your Rails project, just enable the adapter in your test +configuration file: + +```ruby +Rails.application.config.active_job.queue_adapter = :test +```