On Mar 24, 2008, at 8:50 PM, Chuck Remes wrote:
> [snip code]
>
> 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?

Just to show I'm not sitting back and waiting for the answer to be  
handed to me on a silver platter...

I looked through the rspec specs and found a construction that might  
be useful.

In spec/spec/extensions/main_spec.rb we see this:

it "should create an Options object" do
   @main.send(:rspec_options).should  
be_instance_of(Spec::Runner::Options)
   @main.send(:rspec_options).should === $rspec_options
end

In a #before block the @main instance variable was instantiated and a  
module was included. I'm assuming #send is used to check the value of  
a private accessor.

So I could potentially do something like this:

describe ComplexObjectBuilder, "construction" do
   before(:each) do
     @builder = ComplexObjectBuilder.new
     @builder.foo(control1, val1)
     @builder.bar(control2, val2)
   end

   it "should create a YAObject for #foo" do
     @builder.send(:foo).should be_instance_of(YAObject)
   end

   it "should create a ZObject for #bar" do
     @builder.send(:bar).should be_instance_of(ZObject)
   end
end

Is this acceptable? I read through the "specs on private methods"  
thread and the consensus was this should only be done when there is  
*no other option*. Is there another option?

cr
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to