Step 10 - Testing the OrderDetail Business Object

In this step you will test the changes you made to the OrderDetail business object in the previous step.

Test 1: Retrieving Data

In this first test, you will attempt to retrieve order detail for a specific order.

  1. In the OrderSystemTests.cs source code file below the constructor, paste the following GetOrderDetailByOrderID() method:

    [TestMethod]
    public void GetOrderDetailByOrderID()
    {
    	OrderDetail OrderDetail = new OrderDetail();
    	mmBindingList<OrderDetailEntity> OrderDetailList = OrderDetail.GetOrderDetail(10248);
    	foreach (OrderDetailEntity ODEntity in OrderDetailList)
    	{
    		int ProductID = ODEntity.ProductID;
    	}
     
    	Assert.IsTrue(OrderDetailList.Count > 0);
    }

  2. Set a breakpoint in this method by right-clicking the first line of code and selecting Breakpoint | Insert Breakpoint from the shortcut menu.

  3. Press F6 to build the project.

  4. In the Test Explorer, right-click the GetOrderDetailByOrderID node and select Debug. This should cause the method to execute and your breakpoint to be hit:

  5. Press F10 to step over the code that instantiates the OrderDetail business object.

  6. Press F10 again to step over the code that retrieves the OrderDetailList.

  7. Hover your mouse pointer over the OrderDetailList variable, then click the + sign to see the contents of the list. You will see multiple OrderDetailEntity objects:

  8. Let's continue to step through the code to see how you can use a foreach loop to scan through the entity list to examine each entity. Continue to press F10 until the yellow execution point indicator is on the code within the foreach loop:

  9. Hover your mouse pointer over the ODEntity variable and click the + sign to see the properties and property values of the current entity object. If you continue to step through the code by pressing F10, you can examine each of the entity objects in the strongly typed entity list:

  10. When you're done examining the objects, right-click the last line of the method and select Run to Cursor from the shortcut menu. This finishes execution of the foreach loop and highlights the last line of code. This is the line that determines whether the unit test passes or fails. If you hover your mouse pointer over the OrderDetailList.Count variable it should be greater than zero. This means the unit test will pass.

  11. Press F5 to complete execution of the method. The unit test should now be listed in the Test Explorer with a green check mark.

Test 2: Order Detail Business Rules

In this test you intentionally break business rules and see if they are reported back when trying to save data.

  1. Go back to the unit test project and select the OrderSystemTests.cs class to view its code. Add the following method to the class:

    [TestMethod]
    public void TestReportOrderDetailBrokenRules()
    {
    	OrderDetail OrderDetail = new OrderDetail();
    	OrderDetailEntity ODEntity = OrderDetail.NewEntity();
    	mmSaveDataResult Result = OrderDetail.SaveEntity(ODEntity);
    	if (Result == mmSaveDataResult.RulesBroken)
    	{
    		string BrokenRules = OrderDetail.Rules.GetAllBrokenRules();
    	}
     
    	Assert.IsTrue(Result == mmSaveDataResult.RulesBroken);
    }

  2. Set a breakpoint in this method by right-clicking the first line of code and selecting Breakpoint | Insert Breakpoint from the shortcut menu.

  3. Press F5 to build the project. Go to the Test Explorer, right-click the TestReportOrderDetailBrokenRules method, select Debug and you should hit the breakpoint.

  4. Press F10 to instantiate the OrderDetail object, then press F10 again to create a new, empty Entity object. Press F10 once more to attempt to save the new data. Hover your mouse pointer over the Result variable, and it should be set to RulesBroken:

  5. Keep pressing F10 until the yellow execution point indicator is on the ending curly brace of the if statement. To see a list of broken rules, hover your mouse pointer over the BrokenRules variable and click the magnifying glass icon:

  6. This displays the broken rules in a Text Visualizer dialog:

    Click Close to close the Text Visualizer dialog, then press F5 to finish running the test method.

The test is complete. Our business object correctly reported back broken rules when trying to save invalid data!

Test 3: Setting Default Values and Saving

When you generated business objects in an earlier step, you specified default values for the OrderDetail business object. In this test you add a new order detail item and see if the order detail default values get set properly. Afterwards, you make sure the new order detail item gets saved properly.

  1. In the Solution Explorer, click the OrderDetail.cs file to open it. Scroll down to the HookSetDefaultValues() method. This method was automatically created by the Business Layer Generator in response to the default values you specified for the OrderDetail business object. Set a breakpoint in the first line of the HookSetDefaultValues() method by clicking in the margin to the left of the code:

  2. In the Solution Explorer, click the OrderSystemTests.cs file. Add the following method below the TestReportOrderDetailBrokenRules() method:

    [TestMethod]
    public void TestOrderDetailDefaults()
    {
    	OrderDetail OrderDetail = new OrderDetail();
     
    	OrderDetail.TransactionBegin();
     
    	// Call NewEntity() with a new instance of OrderDetailDefaults 
    	// specifying the default Order ID
    	OrderDetailEntity ODEntity = OrderDetail.NewEntity(new OrderDetailDefaults(10248));
     
    	ODEntity.Discount = .10F;
    	ODEntity.ProductID = 16;
    	ODEntity.UnitPrice = 1.23M;
     
    	mmSaveDataResult Result = OrderDetail.SaveEntity(ODEntity);
    	if (Result == mmSaveDataResult.RulesBroken)
    	{
    		string BrokenRules = OrderDetail.Rules.GetAllBrokenRules();
    	}
     
    	OrderDetail.TransactionRollback();
     
    	Assert.IsTrue(Result == mmSaveDataResult.RulesPassed);
    }

  3. Set a breakpoint on the first line of code in the method, then set a second breakpoint on the call to SaveEntity():

  4. Press F5 to rebuild your project. Next, go to the Test Explorer, right-click the TestOrderDetailDefaults() method and select Debug from the shortcut menu. This causes the first breakpoint to be hit.

  5. Press F10 to instantiate an instance of the OrderDetail object. Press F10 again to begin a transaction on the business object. This allows the transaction to be rolled back later in the method so this method can be run over and over again. Press F10 again to call the OrderDetail object's NewEntity() method, passing a new instance of the OrderDetailDefaults object. Notice an order ID of 10248 is passed to the constructor of this object.

  6. You should now hit the breakpoint you set in the HookSetDefaultValues() method of the OrderDetail business object:

    The first line of code checks if the DefaultValues property is null. It isn't, because the NewEntity() method stored the OrderDetailDefaults object into this property.

    The second line of code copies the OrderDetailDefaults OrderID property value to the Entity object's corresponding OrderID property.

    The rest of the code stores the hard-coded default values specified in the Business Layer Generator into the Entity object's properties.

  7. Press F5 to finish executing and exit the method. You should hit the second breakpoint in the TestOrderDetailDefaults() method:

  8. Press F10 to step over the call to SaveEntity(). If everything is working properly, you should be able to hover over the Result variable and see the value is RulesPassed.

  9. Press F5 to finish executing the method and the TestOrderDetailDefaults() method should be listed in the Test Explorer with a green check mark.

In a real-world application, the OrderDetailDefaults object can be used from Windows or Web front ends to pass default values into the NewEntity() method of business objects, or the NewEntity() methods of MM .NET business forms.

See Also:
Step 11 - Testing the Shipper and Employee Business Objects


© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 02/10/26
Comment or report problem with topic