From 39b6840f3c8ea026dae7c4f6c156afabedcefd00 Mon Sep 17 00:00:00 2001 From: Gannon McGibbon Date: Mon, 25 Mar 2019 01:33:53 -0400 Subject: Add section on asserting time args for jobs --- guides/source/testing.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/guides/source/testing.md b/guides/source/testing.md index 93372b5c26..04f0297480 100644 --- a/guides/source/testing.md +++ b/guides/source/testing.md @@ -1733,6 +1733,25 @@ class ProductTest < ActiveSupport::TestCase end ``` +### Asserting Time Arguments in Jobs + +When serializing job arguments, `Time`, `DateTime`, and `ActiveSupport::TimeWithZone` lose microsecond precision. This means comparing deserialized time with actual time doesn't always work. To compensate for the loss of precision, `assert_enqueued_with` and `assert_performed_with` will remove microseconds from time objects in argument assertions. + +```ruby +require 'test_helper' + +class ProductTest < ActiveSupport::TestCase + include ActiveJob::TestHelper + + test 'that product is reserved at a given time' do + now = Time.now + assert_performed_with(job: ReservationJob, args: [product, now]) do + product.reserve(now) + end + end +end +``` + Testing Action Cable -------------------- -- cgit v1.2.3