Resetowanie pojedynczej instancji w Ruby

Jak zresetować obiekt singleton w Ruby? Wiem, że nigdy nie chciałbyś tego robićreal kod, ale co z testami jednostkowymi?

Oto, co próbuję zrobić w teście RSpec -

describe MySingleton, "#not_initialised" do
  it "raises an exception" do
    expect {MySingleton.get_something}.to raise_error(RuntimeError)
  end
end

Nie działa, ponieważ jeden z moich poprzednich testów inicjuje obiekt singleton. Próbowałem postępować zgodnie z radą Iana White'ato link, który zasadniczo małpuje łatki Singleton, aby dostarczyć metodę reset_instance, ale otrzymuję wyjątek niezdefiniowanej metody „reset_instance”.

require 'singleton'

class <<Singleton
  def included_with_reset(klass)
    included_without_reset(klass)
    class <<klass
      def reset_instance
        Singleton.send :__init__, self
        self
      end
    end
  end
  alias_method :included_without_reset, :included
  alias_method :included, :included_with_reset
end

describe MySingleton, "#not_initialised" do
  it "raises an exception" do
    MySingleton.reset_instance
    expect {MySingleton.get_something}.to raise_error(RuntimeError)
  end
end

Jaki jest najbardziej idiomatyczny sposób na zrobienie tego w Ruby?

questionAnswers(3)

yourAnswerToTheQuestion