btakita / rr

RR (Double Ruby) is a test double framework that features a rich selection of double techniques and a terse syntax.
http://github.com/rr/rr
MIT License
500 stars 58 forks source link

Is it possible to mock a method if the method name is in a variable? #58

Closed PatrickLef closed 13 years ago

PatrickLef commented 13 years ago

Is it possible to mock a method if the method name is in a variable?

For example

method_name :my_method
stub(Object).method(method_name).with("foo")

Which I want to work like:

stub(Object).my_method("foo")
mcmire commented 13 years ago

Try:

stub(Object).send(method_name, "foo")

Nothing special, just plain ol' Ruby :)

P.S., it's kind of frowned upon to ask questions in an issue tracker. You'd have better luck pinging Brian directly.

mcmire commented 13 years ago

Actually, that would work, but if I'm reading #28 correctly, calling send on a stub will stub send instead of actually sending the method. You might try

stub(Object).__send__(method_name, "foo")
PatrickLef commented 13 years ago

send seems to make the trick. Yes, I should probably have asked this on stackoverflow or something. It feels wrong to ask the creator of an opens source project since it is more of an question for the whole community.

Is it worth to add this to the readme? Thanks!