Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 798
prosto.vint Avatar asked Apr 29 '26 18:04

prosto.vint


1 Answers

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)
like image 74
Jagdeep Singh Avatar answered May 02 '26 09:05

Jagdeep Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!