Ruby Rspec: checking instance variables without adding source access

I am trying to test the following method:

def unprocess_move(board, move) if move[0].instance_of?(Array) multi_move = @multi_move.pop(2).reverse multi_move.each do |single_move| unapply_move(board, single_move) end else board = unapply_move(board, move) end board end 

where I want to set the state for @multi_move, but I don't want to add an accessory just for testing. Is there any way to do this without accessories? Thank.

+56
ruby instance-variables rspec
Feb 20 '12 at 15:59
source share
1 answer

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

 class Foo def initialize @foo = 5 # no accessor for that variable end end foo = Foo.new puts 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 
+102
Feb 20 '12 at 16:01
source share



All Articles