So I have a complex object that I need to construct. This complex
object, at runtime, takes an object in its initializer. The
initializer interrogates the object and creates several collaborative
objects based upon values from that interrogation.
The construction is complicated enough that it pretty much begs to be
implemented via the Builder Pattern.
My problem is that I don’t know how to test this behavior regardless
of whether the object handles its own construction or I create a
separate Builder object to construct it for me.
@complex_object = ComplexObject.new
def create_foo(control, value)
case control do
when :a then @complex_object.foo = SpecialObject.new(value * 4)
when :v then @complex_object.foo = OtherObject.new
when :z then @complex_object.foo = YAObject.new
def create_bar(control, value)
# similar to above
# some business rules to make sure the object is complete; use
# defaults for fields that were not set via the interface
@builder = ComplexObjectBuilder.new
baz = @builder.construct
How the heck do I test anything here? I do not see how I can validate
the behavior of #create_foo or #create_bar without exposing
@complex_object via a public interface. Those #create_* methods are
purely for construction and do not return a value. Only #construct
returns a value which should be the completed object
Is there a transformation to consider to make this more testable?
I’m hoping someone has tackled this before and can provide me some
insight. Many thanks…