Laurent Francioli
2007-Jul-09 08:27 UTC
[mocha-developer] Problem with Mocks: "random" tests failing
Hi all! I am pretty new to Mocha and I am writing tests that use Mocks quite a bit. I am having a weird behavior with some of the tests for a specific class: I wrote a first couple of tests that were passing and then when adding a 3rd one, the first tests begun to fail. Now what''s bugging me even more is that if I run them in debugger mode with breakpoints on the functions where the errors are, they either pass or fail on other expectations. A few more interesting things are: - These tests all use pretty much the same functions - The class under test launches threads and some of the expectations are within the threads - Only expectations fail like this (all assert always pass) - Commenting "random" lines (like removing an expect on no errors should be logged) can change the outcome of the tests So it looks like either I''m not doing the proper setup/teardown actions or the threads are posing a problem. In my setup I always reload (require) all the libraries I need, and create fresh objects. In the teardown I get rid of the objects under test. I am using the mocha gem 0.5.1 and these problems are really driving me craaaaazy! Please let me know if you ohave any hints for me! I''ll be happy to give more information if required. Thanks alot, Laurent
David Chelimsky
2007-Jul-09 15:21 UTC
[mocha-developer] Problem with Mocks: "random" tests failing
On 7/9/07, Laurent Francioli <laurent.francioli at gmail.com> wrote:> Hi all! > > I am pretty new to Mocha and I am writing tests that use Mocks quite a bit. > I am having a weird behavior with some of the tests for a specific class: > > I wrote a first couple of tests that were passing and then when adding a 3rd > one, the first tests begun to fail. Now what''s bugging me even more is that > if I run them in debugger mode with breakpoints on the functions where the > errors are, they either pass or fail on other expectations. > > A few more interesting things are: > - These tests all use pretty much the same functions > - The class under test launches threads and some of the expectations are > within the threads > - Only expectations fail like this (all assert always pass) > - Commenting "random" lines (like removing an expect on no errors should be > logged) can change the outcome of the tests > > So it looks like either I''m not doing the proper setup/teardown actions or > the threads are posing a problem. In my setup I always reload (require) all > the libraries I need, and create fresh objects. In the teardown I get rid of > the objects under test. > > I am using the mocha gem 0.5.1 and these problems are really driving me > craaaaazy! > > Please let me know if you ohave any hints for me! I''ll be happy to give more > information if required.I think we''d really need specific examples to be able to help you. Is it possible for you to post the code? Both the test code and the code being tested would be most helpful.> > Thanks alot, > Laurent > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer >
James Mead
2007-Jul-10 09:38 UTC
[mocha-developer] Problem with Mocks: "random" tests failing
I suspect this is due to the fact that exceptions raised in one thread are not caught in another. Mocha was not designed to test across multiple threads. In general it is not a good idea to do multi-threaded unit testing. You need to separate out the multi-threaded concern of the code so you can unit test the logic in a single thread. There is an excellent article on the JMock site (http://www.jmock.org/threads.html) that should explain this better - don''t be put off by the fact the examples are in Java - they are very readable. If you are still stuck - perhaps you can do as David suggests and send us a chunk of sample code that you are having difficulty testing. -- James. http://blog.floehopper.org