How to test a Singleton class in Ruby?

Asked

Viewed 86 times

1

I’m using require "Singleton" to make a certain class only be instantiated once. However, when running rspec tests, I get the following error:

Nomethoderror: private method `new' called for Minhaclasse.

I’m not instantiating the class with new, but rather picking up the reference to the object with MinhaClasse.instance. How to solve this problem?

  • You can show the part of the code of your tests that cause the problem (instantiating the class with new)?

1 answer

1


Better show with an example:

# Obs.: Não foi testado
describe MySingletonClass do
  subject { MySingletonClass.instance }
  describe '#to_s' do
    it 'works' do
      expect(subject.to_s).to be eq(subject.to_s)
    end
  end
end

If that doesn’t help, maybe it’s a good idea to share your tests

Browser other questions tagged

You are not signed in. Login or sign up in order to post.