stub an instance variable using mocha

2019-08-26 22:05发布

问题:

Let's say I have a method that references an instance variable directly:

class MyClass
  def method1
    puts @instance_var
  end
end

How can I stub out the value of @instance_var using mocha in a Test::Unit test?

回答1:

You can't. That's one of the many reasons why you should never access ivars directly.