Ruby on Rails Thursday, March 27, 2014

Hi Dave, thanks for taking the time to respond.

Of the two examples below, which do you believe is more testable?

Example 1:

class Car
 def initialise(engine)
   @engine = engine
 end

 def start
   @engine.start
 end
end

Example 2:

class Car
 def initialise
   @engine = Engine.new
 end

 def start
   @engine.start
 end
end



On Thursday, 27 March 2014 02:45:53 UTC+11, Dave Aronson wrote:
On Wed, Mar 26, 2014 at 10:32 AM, masta Blasta <li...@ruby-forum.com> wrote:

> If only the engine class has a method
> called 'start' then Car is effectively coupled to Engine.

Since one could theoretically create another class that supports
#start (and your later examples of #gear_up and #stop, probably
delegating #gear_up to a Transmission-like class), I think it's still
effectively decoupled.  In particular, when *testing* the Car, one
could create it with a MockEngine that's only used in the tests.

--
Dave Aronson, freelance software developer (details @ www.Codosaur.us);
see also www.PullRequestRoulette.com, Blog.Codosaur.us, www.Dare2XL.com

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a4fe62eb-3d94-4fd2-859b-46469585f858%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment