diff --git a/lib/rspec/sidekiq/batch.rb b/lib/rspec/sidekiq/batch.rb index f63ffdd..1d1bb8c 100644 --- a/lib/rspec/sidekiq/batch.rb +++ b/lib/rspec/sidekiq/batch.rb @@ -46,9 +46,17 @@ def failures def join ::Sidekiq::Worker.drain_all - @callbacks.each do |event, callback_class, options| + @callbacks.each do |event, callback, options| if event != :success || failures == 0 - callback_class.new.send("on_#{event}", self, options) + case callback + when Class + callback.new.send("on_#{event}", self, options) + when String + klass, meth = callback.split('#') + klass.constantize.new.send(meth, self, options) + else + raise ArgumentError, 'Unsupported callback notation' + end end end end diff --git a/spec/rspec/sidekiq/batch_spec.rb b/spec/rspec/sidekiq/batch_spec.rb index b8060d8..5eafd46 100644 --- a/spec/rspec/sidekiq/batch_spec.rb +++ b/spec/rspec/sidekiq/batch_spec.rb @@ -58,12 +58,18 @@ class MyCallback def on_event(status, options); end end + class OtherCallback + def foo(status, options); end + end + before(:each) do batch.on(:event, MyCallback, my_arg: 42) + batch.on(:event, 'OtherCallback#foo', my_arg: 23) end it 'executes callbacks' do expect_any_instance_of(MyCallback).to receive(:on_event).with(subject, { my_arg: 42 }) + expect_any_instance_of(OtherCallback).to receive(:foo).with(subject, { my_arg: 23 }) subject.join end end