Rspec 3 stub class variable

3k views Asked by At

anybody know, how I can stub class variable initialization?

I define

@@connection = Bunny.new.start

for restrict connection numbers, but now I have to test it, and when I try stub it in general way, like

allow_any_instance_of(Bunny).to receive(:start) { something }

it doesn't work. @@ variable initializes earlier.

Also, I can redefine it:

before do
  @@connection = double('conn')
end

but it doesn't suit for me because I have to do not allow create a connection to AMQP

SOLVE

I didn't figure out how to do that, so I used bunny-mock, and it helped me

1

There are 1 answers

0
Pascal On

I don't know if it's bad practice but what about:

YourClass.class_variable_set(:@@variable, 'value')

i. e.

before do
 YourController.class_variable_set(:@@connection, 'value')
end