Skip to content

Instantly share code, notes, and snippets.

@chrisbloom7
Created October 2, 2019 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisbloom7/a81a0c224d058b9f4047c1e81d7cd61a to your computer and use it in GitHub Desktop.
Save chrisbloom7/a81a0c224d058b9f4047c1e81d7cd61a to your computer and use it in GitHub Desktop.
Using an anonymous subclass to test singleton objects in isolation
class MySingletonClass
include Singleton
attr_reader :client
def initialize
@client = Service::We::Only::Ever::Need::One::Connection::To.new(SERVICE_URL)
end
end
context MySingletonClass do
setup do
@singleton = Class.new(MySingletonClass)
end
# If we were to test the singleton object directly this test would fail if any other test previously initialized it
test "creates a Service::We::Only::Ever::Need::One::Connection::To connection pointed at SERVICE_URL" do
Service::We::Only::Ever::Need::One::Connection::To.expects(:new).with(SERVICE_URL)
@singleton.instance
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment