[mocha-developer] mocking missing methods

David Chelimsky dchelimsky at gmail.com
Fri Apr 27 19:20:04 EDT 2007


On 4/27/07, Jay Fields <jay at jayfields.com> wrote:
> > 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.
>
> How about:
>
> # instance methods
> sheep = mock
> sheep.restricted_to Sheep.instance_methods
>
> or
>
> # class methods
> sheep = mock
> sheep.restricted_to Sheep.singleton_methods
>
> It's a bit more verbose, but more readable in my opinion.  Also, I'm
> not sold on responds_to or restricted_to.  I don't find that either
> one conveys the intent I'm looking for.
>
> Thoughts?

I love the syntax, though I'd make it a command rather than a "state"-ment:

# instance methods
sheep = mock
sheep.restrict_to Sheep.instance_methods

# class methods
sheep = mock
sheep.restrict_to Sheep.singleton_methods

The other problem is related to Dan's initial point about classes that
are extended or modified at runtime. If you just pass an Array to
#restrict_to then it has no knowledge of the type we're interested in,
so methods added downstream wouldn't be discoverable. With that in
mind, I propose:

# instance methods
sheep = mock
sheep.restrict_to Sheep, :instance_methods

# class methods
sheep = mock
sheep.restrict_to Sheep, :singleton_methods

That keeps the readability Jay proposes and supports the late binding
we're looking for.

More thoughts?


More information about the mocha-developer mailing list