Getting handed over the full text to your ConfirmHandler

For those of you that are coding tests the concept of UI Handlers is probably a known fact. It’s one of the first technical things I explain in my test automation course and I call it the third pillar of the testability framework in Business Central. Each UI Handler type has a specific signature and as such gets handed over parameters by the platform when it is triggered.

When demoing the 5 pillars of the testability framework (see chapter 3 of my book) this is the example I use to explain what a UI Handler is. In this case a MessageHandler.

codeunit 60002 "MyThirdTestCodeunit"
{
    Subtype = Test;

    [Test]
    [HandlerFunctions('MyMessageHandler')]
    procedure MyFirstTestFunction()
    begin
        Message('MyFirstTestFunction');
    end;

    [MessageHandler]
    procedure MyMessageHandler(Message: Text[1024])
    begin

    end;
}

See also MyThirdTestCodeunit.Codeunit.al on the GitHub repo of the book.

When the MyMessageHandler is being triggered it gets handed over, in the Message parameter, the string ‘MyFirstTestFunction’.

In case we would define a message string with a placeholder (you know those %1 things) and a replacing text


    Message('MyFirstTestFunction is ran by %1', UserId());

The Message parameter of MyMessageHandler would contain the string ‘MyFirstTestFunction is ran by LUCAS’ (if LUCAS is the user running the tests).

All fine, as expected. But, as one of the attendees to the current run of my online BC test automation course, Malcolm Gray, pointed out handling a Confirm seems not to yield the expected result. To demo this I changed the above codeunit to. Have a look:

codeunit 60002 "MyThirdTestCodeunit"
{
    Subtype = Test;

    [Test]
    [HandlerFunctions('MyConfirmHandler')]
    procedure MyFirstTestFunction()
    begin
        if Confirm('MyFirstTestFunction is ran by %1', false, UserId()) then;
    end;

    [ConfirmHandler]
    procedure MyConfirmHandler(Question: Text[1024]; var Reply: Boolean)
    begin
        Error('Question= %1', Question);
    end;
}

The error is thrown to show what’s inside the Question parameter. So, when I run the test it fails and displays:

And this is not what Malcolm – and me too – would have expected. Instead of ‘Question = MyFirstTestFunction is ran by %1’ the expected was ‘Question = MyFirstTestFunction is ran by LUCAS’.

Now, Luc, do I have no means of checking against the “full” content? Because that’s what you in general would do in a ConfirmHandler: check the content of Question to verify that the ConfirmHandler was called due to the right Confirm.

Not in this way, but with this following workaround. Instead of using the overloading of Confirm by putting the replacing text as 3rd (or 4th, 5th, etc.) parameter use the StrSubstNo method to set the first parameter:

    Confirm(StrSubstNo('MyFirstTestFunction is ran by %1', UserId()), false)

That’s what we expected.

Thanx, Malcolm! Again a proof that I never stop learning.

Leave a Reply

Your email address will not be published. Required fields are marked *