Ruby Rspec : Testing instance variables without adding an accessor to source Ruby Rspec : Testing instance variables without adding an accessor to source ruby ruby

Ruby Rspec : Testing instance variables without adding an accessor to source


You can use Object#instance_variable_get method to get value of any instance variable of the object like that:

class Foo   def initialize    @foo = 5 # no accessor for that variable  end endfoo = Foo.newputs foo.instance_variable_get(:@foo)#=> 5

And Object#instance_variable_set can be used to set instance variable values:

foo.instance_variable_set(:@foo, 12) puts foo.instance_variable_get(:@foo)#=> 12