Given a class with a couple of instance variables and some methods. Some instance variables are set accessible via attr_reader
and attr_accessor
. Thus the others are private.
Some of the private instance variables get set within one of the instance methods and read/utilized within another method.
For testing I'm using RSpec. As I'm still new to Ruby and want to get all things right, I defined my tests being rather fine-grained. Thus I've got one describe
block for each instance method, which themselves are partitioned into a subset of context
s and it
s. General environmental preconditions are defined with before
.
However, when testing one of the methods, which is utilizing but not setting one of the private variables, I need to call the other method, which is setting this variable. This seems rather overweight and not modular for me.
Is there a way of forcing a private instance variable to a certain value. Similar to "ripping out" the value of a private instance variable with Object::instance_eval(:var)
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…