tags:

views:

15

answers:

1

Is there a hook in NUnit to execute code only when assertion fails without catching the exception itself. Basically, it should accept action delegate to be executed when assertion fails and then re-throw exception. Why do I need this? I need to compare two objects and dump the result on the screen, for easier debugging, when assertion fails.

Something like this works but is a bad hack, The problem is that it eagerly evaluates ProcessCompareError so I have unnecessary overhead, plus it does it no matter if there is an error or not. So, is there overload that will accept the delegate that would be executed when assertion fails?

Assert.That(benefitLimitComparer.Compare(copyBenefitLimit, origBenefitLimit), Is.EqualTo(0),limitError, ProcessCompareError(origBenefitLimit, copyBenefitLimit));
                }
            }
        }

        private string ProcessCompareError(BenefitLimit origBenefitLimit, BenefitLimit copyBenefitLimit)
        {        
            Console.WriteLine("Original: ");
            ObjectDumper.Write(origBenefitLimit);
            Console.WriteLine("Copy");
            ObjectDumper.Write(copyBenefitLimit);

            return "";
        }
A: 

I'm not sure how it might be done through a delegate. One alternative is to store the result of the Compare. If the result is false, write out the contents of the objects and then call Assert.Fail()

Pedro