Skip to content

Commit bfb8096

Browse files
morgothJonRowe
authored andcommitted
Fixed deserialization of passed arguments to with block in AJ matcher (#1684)
1 parent 8f3b707 commit bfb8096

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

lib/rspec/rails/matchers/active_job.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ def supports_block_expectations?
9191
def check(jobs)
9292
@matching_jobs_count = jobs.count do |job|
9393
if serialized_attributes.all? { |key, value| value == job[key] }
94-
@block.call(*job[:args])
94+
args = ::ActiveJob::Arguments.deserialize(job[:args])
95+
@block.call(*args)
9596
true
9697
else
9798
false

spec/rspec/rails/matchers/active_job_spec.rb

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
11
require "spec_helper"
22
require "rspec/rails/feature_check"
3+
34
if RSpec::Rails::FeatureCheck.has_active_job?
45
require "rspec/rails/matchers/active_job"
6+
7+
class GlobalIdModel
8+
include GlobalID::Identification
9+
10+
attr_reader :id
11+
12+
def self.find(id)
13+
new(id)
14+
end
15+
16+
def initialize(id)
17+
@id = id
18+
end
19+
20+
def ==(comparison_object)
21+
id == comparison_object.id
22+
end
23+
24+
def to_global_id(options = {})
25+
@global_id ||= GlobalID.create(self, :app => "rspec-suite")
26+
end
27+
end
528
end
629

730
RSpec.describe "ActiveJob matchers", :skip => !RSpec::Rails::FeatureCheck.has_active_job? do
@@ -26,30 +49,6 @@ def perform; end
2649
end
2750
end
2851

29-
let(:global_id_model) do
30-
Class.new do
31-
include GlobalID::Identification
32-
33-
attr_reader :id
34-
35-
def self.find(id)
36-
new(id)
37-
end
38-
39-
def self.name
40-
"AnonymousClass"
41-
end
42-
43-
def initialize(id)
44-
@id = id
45-
end
46-
47-
def to_global_id(options = {})
48-
@global_id ||= GlobalID.create(self, :app => "rspec-suite")
49-
end
50-
end
51-
end
52-
5352
before do
5453
ActiveJob::Base.queue_adapter = :test
5554
end
@@ -196,7 +195,7 @@ def to_global_id(options = {})
196195
end
197196

198197
it "passes with provided arguments containing global id object" do
199-
global_id_object = global_id_model.new(42)
198+
global_id_object = GlobalIdModel.new("42")
200199

201200
expect {
202201
hello_job.perform_later(global_id_object)
@@ -247,6 +246,17 @@ def to_global_id(options = {})
247246
}
248247
end
249248

249+
it "passess deserialized arguments to with block" do
250+
global_id_object = GlobalIdModel.new("42")
251+
252+
expect {
253+
hello_job.perform_later(global_id_object, :symbolized_key => "asdf")
254+
}.to have_enqueued_job(hello_job).with { |first_arg, second_arg|
255+
expect(first_arg).to eq(global_id_object)
256+
expect(second_arg).to eq({:symbolized_key => "asdf"})
257+
}
258+
end
259+
250260
it "only calls with block if other conditions are met" do
251261
noon = Date.tomorrow.noon
252262
midnight = Date.tomorrow.midnight

0 commit comments

Comments
 (0)