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.) -- Virtually, Ned Wolpert "Settle thy studies, Faustus, and begin..." --Marlowe Discere docendo...
Sorry, Mocha is unlikely to support anything like this. It''s too far away from the idea of mock objects. Can you not refactor the code so that you can write sensible unit tests around the defect? -- James. http://blog.floehopper.org
One would think I could refactor the code, so yes. The optimal way to deal with this is to write the test case that shows the bug, then fix the bug, rather then refactor first. I was hoping that I could use Mocha to help do just that. So it goes... BTW, I enjoy using Mocha more then EasyMock... its a great project. On 4/12/07, James Mead <jamesmead44 at gmail.com> wrote:> Sorry, Mocha is unlikely to support anything like this. It''s too far away > from the idea of mock objects. > > Can you not refactor the code so that you can write sensible unit tests > around the defect? >-- Virtually, Ned Wolpert "Settle thy studies, Faustus, and begin..." --Marlowe Discere docendo...
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.) > >
Yeah, that''s true. What I really want is c.proxy(:do_something).times(2) as a way of calling something that did what you mention below. It wouldn''t be hard to whip up... I just thought it made sense to have it in Mocha... though I do understand its not ''mocking objects''. On 4/14/07, Dan North <dan at tastapod.com> wrote:> 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.) > > > > > > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer >-- Virtually, Ned Wolpert "Settle thy studies, Faustus, and begin..." --Marlowe Discere docendo...