On Feb 7, 2008 2:59 PM, pluskid <pluskid / gmail.com> wrote:
> Hi, all!
>
> I'm wondering how to test some monkey patching code. Take the standard
> library 'jcode'
> in Ruby 1.8 for example. It do some monkey patching for the core
> String class when I
> require it. If I want to write some unit-test or spec for it, how to
> avoid it from affecting other
> tests? In other words, how can I "uninstall" those monkey patching
> after the test case
> is finished or how can I constrain the monkey patching inside a ...,
> say, namespace?
>
> I tried the following code:
>
>
>     orig_string = Object::String.clone
>
>     $KCODE = 'u'
>     require 'jcode'
>
>     # do testings
>
>     Object::String = orig_string


I would try

     orig_string = Object::String
     Object::String = Object::String.clone

     require 'jcode'

     # do testings

     Object::String = orig_string

instead.

Ie, let the modifications happen to a clone, rather than the original.
 That might stop "" from generating a monkey-patched string during
testing, though.

Eivind.