NAV 2009 SP1 – Testing Codeunits – Part 2

In the earlier article, we where using the ConfirmHandler – but confirm is not everything a function can contain. So why not add the handling of Message Boxes 😉

First, let us a add simple test message function to the previous created “doTest” codeunit. The function could look this:

PROCEDURE TestMessage@1112800001(useWrong@1112800000 : Boolean) : Boolean;
BEGIN

   IF useWrong THEN
      MESSAGE('Wrong Message')
   ELSE
      MESSAGE('Test Ok');

   EXIT(TRUE);
END;


It is a simple function that opens a Message Box with either “Wrong Message” or “Test Ok” – depending of the parameter useWrong.

Now we must add the handling of the Message Box to the codeunit “Handler Test Functions”. Here we will be adding the functions:

  • Message_Ok
  • TestMessage_Ok
  • TestMessage_Ok_Error

Message_Ok is the actual MessageHandler. Message_Ok is created in this way:

  1. First add the function to the Global Vars
  2. Change the property “FunctionType” for the Message_Ok function to MessageHandler.

The properties for the Message_Ok function should now look like this:


Function_Message_Ok_Prop

Now it is time to add parameters to the Message_Ok function. The signatur for the MessageHandler is:

Function ( : Text[1024])

Therefore we will add the parameter msg. Next is to add code, that can handle the message. It could look like this:

[MessageHandler]
PROCEDURE Message_Ok@1112800002(Msg@1112800000 : Text[1024]);
BEGIN

  IF Msg <> 'Test Ok' THEN
    ERROR('Unexpected Msg');

END;

Now you can see why I have added 2 different messages to the test function. This is because I would like to test the handling of an unexpected message box.

Ok – the MessageHandler is now in place, so lets look on the functions to handle the Tests.
Let us start by testing an Ok message – which the function TestMessage_Ok will handle.
TestMessage_Ok is created in this way:

  1. First add the function to the Global Vars
  2. Change the property “FunctionType” for the TestMessage_Ok function to Test.
  3. Change the property “HandlerFunctions” to “Message_Ok”

The properties for the TestMessage_Ok function should now look like this:


Function_TestMessage_Ok_Prop

All you now have to do – is calling the TestMessage function in the doTest codeunit. You should end up with a function like this:

[Test]
[HandlerFunctions(Message_Ok)]
PROCEDURE TestMessage_Ok@1112800006();
VAR
   doTest@1112800000 : Codeunit 50001;
BEGIN

   IF NOT doTest.TestMessage(FALSE) THEN
      ERROR('Error in test - Ok');
END;

If you repeats these steps to create the function TestMessage_Ok_Error – you could end up with a function like this:
 

[Test]
[HandlerFunctions(Message_Ok)]
PROCEDURE TestMessage_Ok_Error@1112800007();
VAR
   doTest@1112800000 : Codeunit 50001;
BEGIN

   IF NOT doTest.TestMessage(TRUE) THEN
      ERROR('Error in Test - Ok Error');
END;

When you now run the “Test Runner” codeunit – the TestMessage function in to codeunit doTest will be tested.

If you have noticed, you can see that the function TestMessage_Ok_Error sends in a TRUE – which means, that the doTest.TestMessage will show the Message Box containing the message “Wrong Message”. The MessageHandler function though, only handles the message “Test Ok”. Therefor you will in the log see, that the first function TestMessage_Ok went well and that the second function TestMessage_Ok_Error failed.

The Log could look like this:


Log-MessageHandler
That’s all folks – now you also can handle Message Boxes 🙂

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.