Testing of Errors and Warnings: To Test the Display of Errors and Warnings
The following activity will walk you through the process of creating a method that tests the display of warning and error messages.
Story
Suppose that you want to make sure that the following behavior of the Repair Work Orders (RS301000) form has not changed:
- When a new work order is initialized, the system copies to it the repair items
and labor items of the
RSSVRepairPriceobject that has the sameDeviceIDandServiceIDvalues as the work order being initialized. - Negative values of labor quantities are prohibited.
- The values of labor quantities are not permitted to be less than the minimum values.
- In work orders for which the priority is Low, repair services that are marked as requiring preliminary checks are prohibited.
You need to create a test method. In this method, you need to initialize the settings of the graph.
Process Overview
You will create a test method without parameters for the Repair Work Orders
(RS301000) form. In this method, you will initialize the settings of the tested
graph RSSVWorkOrderEntry by calling the Setup<>
generic method. Then you will create an instance of the
RSSVWorkOrderEntry graph. In the cache of the graph, you will
create the necessary objects and configure them. You will assign to the fields
values that are not allowed by the business logic of the graph and ensure that the
appropriate warnings or errors appear in the UI; you will do this by calling the
PXCache.GetStateExt method.
System Preparation
- Make sure that you have performed the Test Instance for Unit Testing: To Deploy an Instance prerequisite activity to prepare the MYOB Acumatica instance that you will use.
- Make sure that you have performed the Test Project and Test Class: To Create a Test Project prerequisite
activity to create and configure the
PhoneRepairShop_Code.Tests.csprojtest project. - Create the
RSSVWorkOrderEntryTeststest class. For an example that shows how to create a test class, see Test Project and Test Class: To Create a Test Class.
Step 1: Creating a Test Method for the Repair Work Orders Form
In this step, you will create a test method for the Repair Work Orders (RS301000) form. (For basic information on the creation of a test method without parameters, see Test Method: General Information.) Do the following:
- In the
RSSVWorkOrderEntryTestsclass, create apublic voidmethod, and name itTestRepairWorkOrdersForm. - Specify the
[Fact]attribute for the method to indicate that this unit test is called without parameters. - Initialize the settings for the
RSSVWorkOrderEntrygraph by adding the following code.Setup<RSSVWorkOrderEntry>(new RSSVSetup());
The
RSSVSetupDAC contains settings for theRSSVWorkOrderEntrygraph. The call of the Setup<> generic method is mandatory because it initializes anRSSVSetupobject. - Create an instance of the
RSSVWorkOrderEntrygraph as follows.var graph = PXGraph.CreateInstance<RSSVWorkOrderEntry>();
Step 2: Creating the Necessary Objects
In the TestRepairWorkOrdersForm method, create the objects that are
needed to test the displaying of warnings and errors as follows:
- Create an
RSSVDeviceobject and twoRSSVRepairServiceobjects (one of these is the main object, and the other is auxiliary) by adding the following code.RSSVDevice device = (RSSVDevice)graph.Caches[typeof(RSSVDevice)]. Insert(new RSSVDevice { DeviceCD = "Device1" }); RSSVRepairService repairService = (RSSVRepairService)graph.Caches[typeof(RSSVRepairService)]. Insert(new RSSVRepairService { ServiceCD = "Service1" }); RSSVRepairService repairService2 = (RSSVRepairService)graph.Caches[typeof(RSSVRepairService)]. Insert(new RSSVRepairService { ServiceCD = "Service2" });
- Create an
RSSVRepairPriceobject, and initialize it with theRSSVDeviceobject and the mainRSSVRepairServiceobject. You do this by adding the following code.graph.Caches[typeof(RSSVRepairPrice)].Insert(new RSSVRepairPrice { DeviceID = device.DeviceID, ServiceID = repairService.ServiceID }); - Make sure that you have added the
using PX.Objects.IN;directive to theRSSVWorkOrderEntryTestsclass. - Add the following code to create two stock items and one non-stock item, and to
make the two stock items repair items.
InventoryItem battery1 = (InventoryItem)graph.Caches[typeof(InventoryItem)].Insert(new InventoryItem { InventoryCD = "Battery1" }); InventoryItemExt batteryExt1 = battery1.GetExtension<InventoryItemExt>(); batteryExt1.UsrRepairItem = true; batteryExt1.UsrRepairItemType = RepairItemTypeConstants.Battery; graph.Caches[typeof(InventoryItem)].Update(battery1); InventoryItem backCover1 = (InventoryItem)graph.Caches[typeof(InventoryItem)].Insert(new InventoryItem { InventoryCD = "BackCover1" }); InventoryItemExt backCoverExt1 = backCover1.GetExtension<InventoryItemExt>(); backCoverExt1.UsrRepairItem = true; backCoverExt1.UsrRepairItemType = RepairItemTypeConstants.BackCover; graph.Caches[typeof(InventoryItem)].Update(backCover1); InventoryItem work1 = (InventoryItem)graph.Caches[typeof(InventoryItem)].Insert(new InventoryItem { InventoryCD = "Work1", StkItem = false }); - Create two repair items based on the two stock items as
follows.
RSSVRepairItem repairItemBackCover1 = (RSSVRepairItem)graph.Caches[typeof(RSSVRepairItem)].Insert( new RSSVRepairItem { DeviceID = device.DeviceID, ServiceID = repairService.ServiceID }); repairItemBackCover1.InventoryID = backCover1.InventoryID; repairItemBackCover1.Required = true; repairItemBackCover1.BasePrice = 10; repairItemBackCover1.IsDefault = true; repairItemBackCover1.RepairItemType = backCoverExt1.UsrRepairItemType; graph.Caches[typeof(RSSVRepairItem)].Update(repairItemBackCover1); RSSVRepairItem repairItemBattery1 = (RSSVRepairItem)graph.Caches[typeof(RSSVRepairItem)].Insert( new RSSVRepairItem { DeviceID = device.DeviceID, ServiceID = repairService.ServiceID }); repairItemBattery1.InventoryID = battery1.InventoryID; repairItemBattery1.Required = true; repairItemBattery1.BasePrice = 20; repairItemBattery1.IsDefault = true; repairItemBattery1.RepairItemType = batteryExt1.UsrRepairItemType; graph.Caches[typeof(RSSVRepairItem)].Update(repairItemBattery1); - Create an
RSSVLaborobject based on the non-stock item by using the following code.RSSVLabor labor = (RSSVLabor)graph.Caches[typeof(RSSVLabor)]. Insert(new RSSVLabor { InventoryID = work1.InventoryID, DeviceID = device.DeviceID, ServiceID = repairService.ServiceID }); labor.DefaultPrice = 2; labor.Quantity = 3; graph.Caches[typeof(RSSVLabor)].Update(labor);
- Create a work order with the same
DeviceIDandServiceIDvalues as those specified for theRSSVRepairPriceobject, which was created in Instruction 2. To do this, add the following code.RSSVWorkOrder workOrder = (RSSVWorkOrder)graph. Caches[typeof(RSSVWorkOrder)].Insert(new RSSVWorkOrder()); workOrder.DeviceID = device.DeviceID; workOrder.ServiceID = repairService.ServiceID; graph.Caches[typeof(RSSVWorkOrder)].Update(workOrder);
Step 3: Testing the Display of Errors and Warnings
Now that the necessary objects have been created, perform the following instructions to test the display of errors and warnings:
- As the first test, add the following code to make sure that there are two
RSSVWorkOrderItemobjects, which have been created based on the twoRSSVRepairItemobjects, and anRSSVWorkOrderLaborobject, which has been created based on theRSSVLaborobject.Assert.Equal(2, graph.RepairItems.Select().Count); Assert.Single(graph.Labor.Select()); - As the second test, add the following code to ensure that the changing of the
ServiceIDfield of the work order does not affect theRepairItemsandLaborviews.workOrder.ServiceID = repairService2.ServiceID; graph.Caches[typeof(RSSVWorkOrder)].Update(workOrder); Assert.Equal(2, graph.RepairItems.Select().Count); Assert.Single(graph.Labor.Select()); - Restore the
ServiceIDvalue as follows.workOrder.ServiceID = repairService.ServiceID; graph.Caches[typeof(RSSVWorkOrder)].Update(workOrder); - Obtain the
RSSVWorkOrderLaborobject (the value of itsQuantityfield must be 3), and try to assign a negative value to itsQuantityfield by using the following code. An error message must be attached to theQuantityfield instead.RSSVWorkOrderLabor woLabor = graph.Labor.SelectSingle(); Assert.Equal(3, woLabor.Quantity); woLabor.Quantity = -1; graph.Caches[typeof(RSSVWorkOrderLabor)].Update(woLabor); PXFieldState fieldState = (PXFieldState)graph.Caches[typeof(RSSVWorkOrderLabor)]. GetStateExt<RSSVWorkOrderLabor.quantity>(woLabor); Assert.Equal(PhoneRepairShop.Messages.QuantityCannotBeNegative, fieldState.Error); Assert.Equal(PXErrorLevel.Error, fieldState.ErrorLevel); graph.Labor.Cache.Clear();In the code, the
PXCache.Clearmethod is called to clear the cache of the generated error. - By using the following code, assign to the
Quantityfield of theRSSVWorkOrderLaborobject a value that is less than the value of the correspondingRSSVLabor.Quantityfield. Then make sure that the following result is achieved: A warning message is attached to theRSSVWorkOrderLabor.Quantityfield, and the field is assigned the value of the correspondingRSSVLabor.Quantityfield.woLabor.Quantity = 1; graph.Caches[typeof(RSSVWorkOrderLabor)].Update(woLabor); woLabor = (RSSVWorkOrderLabor)graph. Caches[typeof(RSSVWorkOrderLabor)].Locate(woLabor); fieldState = (PXFieldState)graph. Caches[typeof(RSSVWorkOrderLabor)]. GetStateExt<RSSVWorkOrderLabor.quantity>(woLabor); Assert.Equal(PhoneRepairShop.Messages.QuantityTooSmall, fieldState.Error); Assert.Equal(PXErrorLevel.Warning, fieldState.ErrorLevel); Assert.Equal(3, woLabor.Quantity);
In the code, you use the
PXCache.Locatemethod to obtain the properRSSVWorkOrderLaborobject from the cache to check the presence and text of the warning. - By using the following code, configure the second repair service
repairService2to require a preliminary check, and assign it to the work order. The code also sets the priority of the work order to Low and makes sure that an error message is attached to thePriorityfield of the work order; this error message informs the user that the priority of the work order is too low.repairService2.PreliminaryCheck = true; graph.Caches[typeof(RSSVRepairService)].Update(repairService2); workOrder.ServiceID = repairService2.ServiceID; workOrder.Priority = WorkOrderPriorityConstants.Low; graph.Caches[typeof(RSSVWorkOrder)].Update(workOrder); workOrder = (RSSVWorkOrder)graph.Caches[typeof(RSSVWorkOrder)]. Locate(workOrder); fieldState = (PXFieldState)graph.Caches[typeof(RSSVWorkOrder)]. GetStateExt<RSSVWorkOrder.priority>(workOrder); Assert.Equal(PhoneRepairShop.Messages.PriorityTooLow, fieldState.Error); Assert.Equal(PXErrorLevel.Error, fieldState.ErrorLevel); - Run the test method you have created, and make sure that it succeeds.
