[mocha-developer] mocking missing methods

David Chelimsky dchelimsky at gmail.com
Wed Apr 25 19:19:50 EDT 2007


On 4/25/07, James Mead <jamesmead44 at gmail.com> wrote:
> Hmm. Interesting. I think I find my examples more readable/logical
> than the ones you give, but it's helpful to have someone else's take
> on the matter.
>
> Also wouldn't there be difficulties implementing the
> responds_like(Sheep.class) version, in that the class() method on any
> class object will return Class e.g. Sheep.class # => Class and
> Goat.class # => Class.

Bummer - was just thinking about the outside view - this does present a problem.

>
> An example I forgot to mention...
>
> sheep = mock()
> sheep.responds_like(Sheep.new)
>
> So responds_like(responder) just needs a responder that responds to
> respond_to?() if you see what I mean. The instance_of() method would
> be provided so you didn't have to construct an instance of an object
> just so you could mock it.

I think the thing that led me to Sheep/Sheep.class is the lack of
symmetry that instance_of(Sheep) introduces. I guess I'd rather have
the instance be the default and have a special way to indicate that
I'm interested in class methods:

sheep = mock()
sheep.responds_like(Sheep)

sheep_class = mock
sheep_class.responds_like(class_of(Sheep))

I don't like #class_of, but you get the idea.

How about this?

sheep = mock()
sheep.responds_like(Sheep, :instance)

sheep_class = mock
sheep_class.responds_like(Sheep, :class)

... and have :instance be the default so you can say
responds_like(Sheep) for instances?????

Or this?

sheep = mock()
sheep.responds_like(Sheep)

sheep_class = mock
sheep_class.responds_like_class(Sheep)

Actually, I think this would be the easiest to implement, too.
#responds_like(klass) could ask for klass's public_instance_methods
and #responds_like_class(klass) could ask for klass's
public_class_methods.

If, on the other hand, supporting instance_of(Sheep),
responds_like(responder) has to treat responder differently if it is
(instance_of(Sheep)) or just (Sheep). In the case of just Sheep, it
could ask for it's public_class_methods. In the case of
instance_of(Sheep) it would have to ask for something else - or you'd
have to override public_class_methods on the object returned by
instance_of(Sheep) to return the sheep's public_instance_methods -
which just doesn't seem right to me.

WDYT?

David
>
> --
> James.
> http://blog.floehopper.org
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer
>


More information about the mocha-developer mailing list