[mocha-developer] Test errors without verification

Zach Moazeni zach.lists at gmail.com
Thu Jul 26 07:20:48 EDT 2007


That's an interesting article. I haven't come across that, but I do  
something very similar when testing validations in ActiveRecord. The  
problems I encounter more often are testing an object in isolation  
where I'll have a scenario similar to the following.

class Car
   def intialize(engine)
     @parts = []
     @parts << engine\

   def start
     # run though a ton of part and return whether the car started
     # typically there would be error handling, or you would just AND  
all the parts.start
     started = false
     @parts.each do | part |
        started = started && part.start


def test_start_the_car
   engine_mock = mock("engine")
   car = Car.new(engine_mock)

   assert car.start

This is a trivial example I just made up of the top of my head, but  
you can imagine if the code at Car#start did not call Engine#start,  
and that resulted in the assertion failure. The test does not give as  
much feedback as it could to help the developer determine what went  

In my opinion its quicker to debug if both errors / failures are  
reported and you have a little more to go off on rather than having  
to dig into the code / tests immediately to try and locate where the  
point of failure occurred.

If you'd still rather the designed behavior, would it be possible to  
propose an alternate patch that looks similar to the following

class VerboseFailuresTest < Test::Unit::TestCase

   mocha_force_verifications = true

   def test_foo

which has the default behavior of false (to accommodate the original  


On Jul 26, 2007, at 5:12 AM, James Mead wrote:

> Hi Zach,
> On 26/07/07, Zach Moazeni <zach.lists at gmail.com> wrote:
>> I'm not sure if this is by design, but I've stumbled across this a
>> few times trying to debug my own tests. If an assertion fails the
>> test, and a missing mock expectation was to blame, the test's
>> failure / error messages don't give enough info.
> This is by design. In my mind a verification failure is analogous  
> to an
> assertion failure. Test::Unit does not report multiple assertion  
> failures
> per test, but reports the first failure. Failing fast seems like  
> the right
> thing to do. At that point all bets are off and there is no point in
> continuing.
> I wonder if the pain you are feeling is because you have too many  
> assertions
> and/or expectations in each test. You might find the following article
> interesting...
> http://blog.jayfields.com/2007/06/testing-one-assertion-per-test.html
> Thanks for taking the time to put a patch together, but at this  
> stage I'm
> not convinced it's the way to go.
> -- 
> James.
> http://blog.floehopper.org
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer

More information about the mocha-developer mailing list