[mocha-developer] Feature request... I think...

Dan North dan at tastapod.com
Sat Apr 14 15:10:32 EDT 2007


You could try something like this:

def test_check_state_of_b
  class B
    alias :original_do_something :do_something
    def do_something(*args)
      # pre-checks here
      original_do_something(*args)
      # post-checks here
    end
  end
 
  # body of test here
 
  class B
    alias :do_something :original_do_something
  end
end

It's evil, but it works. In fact if you inject the instance of b into a, 
you could just doctor the specific instance of b rather than the whole B 
class.

Cheers,
Dan


Ned Wolpert wrote:
> Folks-
>
> I just started using Mocha and I wanted to try something but I
> couldn't get it to work.  I'm coming from the Java world, so my
> approach may not be optimal for Ruby.
>
> Say I'm testing a.do_something() which calls b.do_other_thing() twice,
> but I really don't want to change b.do_other_thing(), just 'expect' it
> to be called twice.  I know I can b.expect(:do_other_thing).times(2)
> but it changes the whole method.  Unless I'm doing something wrong, I
> can't do this in mocha.
>
> I know its not mocking the object or method.  But what I really want
> to do is put in a hook into validate the state of the object at the
> point of time into and out of the method.  If this isn't the place of
> mocha, where should I do it?
>
> (And yes, one can easily argue that I'm likely making my test case too
> complicated if I have to do this, but we found an esoteric bug on a
> short-lived object and having a test to check for this specific case
> would help make sure that bug stays away.)
>
>   



More information about the mocha-developer mailing list