Jump to content
luebbe

Is there a way to make DUnitX ignore exceptions?

Recommended Posts

Hi Folks,

 

currently I'm trying to add a default exception handler to our logging framework. I wanted to add a test case to DUnitX using a mock logchannel. The problem I'm facing is that DUnitX already catches the exception so that it never gets delivered to the logger.

When I wrap the exception into Assert.WillRaise() DUnitX is happy, but the mock verification fails, because g_Logger.DoOnException never gets called and thus the message never gets delivered to the channel. If I don't wrap it, DUnitX catches the exception and the test fails.

 

Is this possible at all? Am I heading down a totally wrong path?

 

procedure TestLogChannel.TestReceivesAnyException;
var
  MC: TMock<ILogChannel>;
begin
  Application.OnException := g_Logger.DoOnException;

  // Set up the mock log channel
  MC := TMock<ILogChannel>.Create;

  try
    g_Logger.RegisterChannel(MC);

    MC.Setup.Expect.Once('Deliver');       // Check whether a message has been delivered to this channel

//    Raise EDivByZero.Create('Thou shalt not do this');

    Assert.WillRaise(
      procedure
      begin
        Raise EDivByZero.Create('Thou shalt not do this');
      end, EDivByZero);

    Assert.WillNotRaise(
      procedure
      begin
        MC.Verify;
      end, EMockVerificationException);

    g_Logger.UnregisterChannel(MC);

  finally
    MC.Free;
  end;
end;

 

Share this post


Link to post

Clearly you need to write your own WillRaise version that does not just swallow the exception but passes it on to the logger.

Share this post


Link to post

I'm not familiar with Delphi-Mocks so I might be missing something, but I don't understand why you're wiring up your 

g_Logger.DoOnException

to 

Application.OnException

. It will never get called anyway. Either your test case will catch the exception, or DUnitX will catch it. Not sure, but this probably won't even work in a console application either.

 

What you're actually trying to test is if the VCL's 

Application.OnException

event is being called when an exception in the main thread is not caught by anything and goes up all the way to the VCLs outermost exception handler. This should not be a concern of your logging framework. You can test your handler, of course, but not core parts of the RTL/VCL.

Edited by Der schöne Günther

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×