简体   繁体   English

如何在方法中存根类变量?

[英]How to stub class variable in method?

Need help ;) Here is example of the code: 需要帮助;)以下是代码示例:

def some_method
  @some_variable = "some logic for a few strings and operations"
end

I want to stub this method like a 我想将此方法存根为

controller.stub(:some_method).and_return( ??? )

But @some_variable should be defined also, how I can do that? 但@some_variable也应该定义,我该怎么做?

You can stub it like 你可以像它一样存根

allow(controller).to receive(:some_method) { your_return_value }

Just using the new syntax for stubbing above. 只需使用上面的存根新语法。

For your instance variable, you can do 对于您的实例变量,您可以这样做

controller.instance_variable_set(:@some_variable, value)

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

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