Skip to content

Instantly share code, notes, and snippets.

@tylerhunt
Created August 7, 2015 18:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerhunt/3decf5a32873625110c1 to your computer and use it in GitHub Desktop.
Save tylerhunt/3decf5a32873625110c1 to your computer and use it in GitHub Desktop.
RSpec shared context to allow testing of Active Job objects with GlobalID-compatible doubles.
RSpec.shared_context 'Global ID', :global_id do
def global_id_instance_double(doubled_class, stubs={})
instance_double(doubled_class, stubs)
.extend(GlobalID::Identification)
.tap { |double|
unless double.respond_to?(:id)
allow(double).to receive(:id).and_return(double.object_id)
end
}
end
before do
allow(GlobalID).to receive(:app).and_return('example') unless GlobalID.app
end
end
@Startouf
Copy link

Startouf commented Apr 30, 2017

Thanks for the Gist ! Works perfect to make those ActiveJob jobs works with RSpec doubles.

Saved in spec/support/global_id_context.rb, I added to that file (following https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-context)

RSpec.configure do |rspec|
  rspec.include_context 'Global ID', include_shared: true
end

And call it from other tests with (using RSpec 3.5)

describe 'Foo' do 
  include_context 'Global ID'
  let(:collaborator_with_globalID) { global_id_instance_double(CollaboratorClass) } 
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment