VerifyCustomerCategoryOnCustomer

Each test needs to verify its outcome. Put bluntly, a test without verification is not a test. And for the current test, we need to verify that the customer category code that is assigned to the Customer Category Code_PKT field of the customer record is indeed the value that was created in the Customer Category table. We therefore retrieve the record from the database and verify the content of the Customer Category Code_PKT field as follows:

local procedure VerifyCustomerCategoryOnCustomer(
CustomerNo: Code[20]; CustomerCategoryCode: Code[20])
var
Customer: Record Customer;
FieldOnTableTxt: Label '%1 on %2';
begin
with Customer do begin
Get(CustomerNo);
Assert.AreEqual(
CustomerCategoryCode,
"Customer Category Code_PKT",
StrSubstNo(
FieldOnTableTxt,
FieldCaption("Customer Category Code_PKT"),
TableCaption())
);
end;
end;

To verify that the expected value (first argument) and the actual value (second argument) are equal, we make use of the AreEqual function in the standard library Assert codeunit (130000). Of course, we could build our own verification logic using the error system function, and that's what AreEqual is doing too. Have a look at the following code:

[External] procedure AreEqual(Expected: Variant;
Actual: Variant;Msg: Text)
begin
if not Equal(Expected,Actual) then
Error(
AreEqualFailedMsg,
Expected,
TypeNameOf(Expected),
Actual,
TypeNameOf(Actual),
Msg)
end;

By using the AreEqual function, however, we ensure that we get a standardized error message in case the expected and actual values are not equal. Over time, when reading the error of any failing test, you will be able to easily recognize the kind of error that occurred, given that your verification helper functions make use of the Assert library.

The completed test function would look like the following code, which is ready for execution:

[Test]
procedure AssignNonBlockedCustomerCategoryToCustomer()
// [FEATURE] Customer Category
var
Customer: Record Customer;
CustomerCategoryCode: Code[20];
begin
// [SCENARIO #0001] Assign non-blocked customer category to
// customer
// [GIVEN] A non-blocked customer category
CustomerCategoryCode := CreateNonBlockedCustomerCategory();
// [GIVEN] A customer
CreateCustomer(Customer);
// [WHEN] Set customer category on customer
SetCustomerCategoryOnCustomer(Customer, CustomerCategoryCode);
// [THEN] Customer has customer category code field populated
VerifyCustomerCategoryOnCustomer(
Customer."No.",
CustomerCategoryCode);
end;

Notice the variables and arguments that have been added to the test codeunit and functions.

Go to the GitHub repository for this book to have a look at the full implementation of the test codeunit: https://github.com/PacktPublishing/Mastering-Microsoft-Dynamics-365-Business-Central/tree/master/Chapter%2010/TestPacktDemoExtension/Src/testcodeunit.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.66.206