From: Robert Klemme Date: 2005-03-15T23:31:18+09:00 Subject: Re: Mocking new and other class methods "jeem" schrieb im Newsbeitrag news:1110841586.340637.7980@g14g2000cwa.googlegroups.com... > Sorry, I'll try to clarify. > > When I type this: > > mn=MockNew.new(A) > mn.handle { block } > > I'm saying, "Next time someone calls A.new(args), execute this block > instead." This lets me test code like this: > > def methodToTest > etc > a = A.new('bob') > a.etc > end > > Usually I want to find some other way to test code like this, but if > A.new or a.etc hits the file system or network, etc., I really want to > be able to sub in a mock. Often we deal with this by passing in the > object, thus isolating the object creation code. This still leaves the > creation code untested, and a good place for bugs to hide. Well, my example is easily modified to do that also, just add def handle(&b) @handlers << b if b end My main point was, that you don't have to mess with the original class instance. You can just use any object as stand in that implements #new. Kind regards robert