简体   繁体   English

Rspec 3存根类变量

[英]Rspec 3 stub class variable

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 但这不适合我,因为我不允许创建与AMQP的连接

SOLVE 解决

I didn't figure out how to do that, so I used bunny-mock, and it helped me 我不知道该怎么做,所以我使用了兔子模型,它对我有帮助

I don't know if it's bad practice but what about: 我不知道这是否不好,但是:

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

ie

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM