Created
October 2, 2019 02:57
-
-
Save chrisbloom7/a81a0c224d058b9f4047c1e81d7cd61a to your computer and use it in GitHub Desktop.
Using an anonymous subclass to test singleton objects in isolation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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