Step 8 : Testing the Order Business Object

In this step you will test the Order business object using Visual Studio's built-in unit test capabilities.

Adding a Unit Test Project to the Solution

  1. In the Solution Explorer, right-click the Solution node and select Add | New Project from the shortcut menu.

  2. In the panel on the left in the Add New Project dialog, select Visual Basic. Next, select the MM .NET EF Test VB template, then in the Name text box at the bottom of the dialog, specify Order System Business Objects EF Code First VB Tests and then click the OK button:

  3. In the MM .NET New Project dialog, set the Default Namespace to Acme.OrderSystem.BusinessEF.Tests:

    Click OK to continue. This adds the new project to the Solution Explorer:


Adding Assembly References to the Unit Test Project

Now we need to add a reference to the business object project.

  1. In the Solution Explorer, right-click the References node in the new Unit Test project and select Add Reference... from the shortcut menu.

  2. On the left side of the Reference Manager dialog under the Projects node, select Solution. Then, in the center panel, click the check box next to the Order System Business Objects EF Code First VB box to select it:

    Click OK to close the dialog.

Copying the app.config File to the Unit Tests Project

Before running the test, we need to copy the app.config file from the Order System Business Objects EF Code First VB project to the Order System Business Objects Test VB project. This is necessary so that the Entity Framework can get access to the connection string stored in the app.config file.

To do this, all you have to do is click the app.config file and drag it down into the unit test project. When prompted, overwrite the existing app.config file. This makes a copy of the app.config file and adds it to the unit test project:


Test 1: Retrieving Data

Now you're ready to rename the test class and create a test method.

  1. In the Solution Explorer, right-click the UnitTests.vb file and select Rename from the shortcut menu. Change the name of the file to OrderSystemTests.vb. When you do this, a dialog is displayed asking if you want to rename all references to the code element. Click the Yes button:

    This renames the file and the class that it contains to OrderSystemTests.

  2. Next, uncomment the code in the class constructor:

    <TestClass()> Public Class OrderSystemTests
     
    	''' <summary>
    	''' Constructor
    	''' </summary>
    	Public Sub New()
    		mmAppBase.Factory = New AAppTestFactory()
    	End Sub

    This code instantiates an instance of the AAppTestFactory class, which was automatically included in the MM .NET Entity Business Object project when you created it.

  3. Now add the following Imports statement to the top of the code file:

    Imports Acme.OrderSystem.BusinessCF
    

  4. Next, rename the TestMethod method to GetOrderByOrderID, and then add the code to the method shown here:

    <TestMethod()>
    Public Sub GetOrderByOrderID()
     
    	Dim Order As Order = New Order()
    	Dim orderEntity As OrderEntity = Order.GetOrderByOrderID(10248)
    	Assert.IsTrue(orderEntity.OrderID = 10248)
     
    End Sub

    This is a very simple test to check that our new GetOrderByOrderID method is working properly. All we do is create a new Order object and then run the method, asking to return order 10248. The Assert then checks to make sure the order we requested was retrieved.

  5. Now build the project by pressing F6 or or by going to the menu and selecting Build | Build Solution.

  6. Create a breakpoint in the test method by clicking to the left of the Assert statement:

    This will give us an opportunity to see what's happening when we run the unit test.

  7. If the Test Explorer isn't visible, go to the Visual Studio menu and select View | Test Explorer.

    You should see the GetOrderByOrderID test method listed in the Test Explorer:

  8. Right-click the test method and select Debug from the shortcut menu. After a few seconds, execution should stop at the breakpoint you set:

    If you look at the Autos window, you should see the orderEntity.orderID property is set to 10248. This means our test will pass!

  9. To continue, click the Continue button in the Visual Studio toolbar or just press F5. When the test completes, you should see a green check mark next to the test in the Test Explorer:


Test 2: Saving Data

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

  1. Add the following new test method to the OrderSystemTests class:

    <TestMethod()>
    Public Sub TestOrderRules()
     
    	Dim Order As Order = New Order()
    	Dim OrderEntity As OrderEntity = Order.GetOrderByOrderID(10248)
    	OrderEntity.CustomerID = ""
    	OrderEntity.ShipName = ""
    	Dim result As mmSaveDataResult = Order.SaveEntity(OrderEntity)
     
    	If result = mmSaveDataResult.RulesBroken Then
    		Dim brokenRules As String = Order.Rules.GetAllBrokenRules()
    	End If
     
    	Assert.IsTrue(result = mmSaveDataResult.RulesBroken)
     
    End Sub

    This code retrieves an OrderEntity object, clears the CustomerID and ShipName properties (both of which you have specified are required pieces of information), and then tries to save the entity. If the rules are broken, then a brokenRules string is created, which contains a list of the broken rules.

  2. Click in the gutter to the left of the line of code that calls GetAllBrokenRules() to set a breakpoint.

  3. Build the project, and then go to the Test Explorer. You should see the new test method:


  4. Right-click the test and select Debug from the shortcut menu.

  5. After you hit the breakpoint, click the Step Over button and you should see the brokenRules string variable displayed in the Autos window:

  6. Click the Continue button to finish executing the test. Afterwards, you should see the TestOrderRules method displayed under Passed Tests:

    See Also:
    Step 10 : Enhancing the OrderDetail Business Object


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