Skip to content

Tutorial 05 01 Unit Testing Generated OData Services

Steve Ives edited this page May 31, 2020 · 24 revisions

Harmony Core Logo

Tutorial 5: Unit Testing Generated OData Services

IMPORTANT: THIS TUTORIAL IS A WORK IN PROGRESS AND IS NOT YET COMPLETE. PLEASE DO NOT ATTEMPT TO USE THIS TUTORIAL UNTIL THIS ANNOUNCEMENT IS REMOVED.

In this tutorial, you will learn how to generate, configure, and run unit tests for the OData controllers and endpoints that you are generating.

In order to complete this tutorial, you must have an existing Harmony Core solution that contains code-generated OData services, that you created by following either the Creating a Demo Service or Building a Service From Scratch tutorials.

The unit testing environment produced by this tutorial also supports custom authentication, so if you have also followed the Authentication via Custom Code, that's OK, things should still work here.

Add and Configure a Unit Testing Project

The first step in the process of adding unit testing capabilities to your Harmony Core development environment is to add a new project to your solution. In .NET Core unit tests are typically executed from the command line, using the command:

dotnet test

So to facilitate this, the new project that you will add to your solution will be a Synergy .NET Core Console Application. The name of the project isn't important, but for consistency with the rest of your solution, we suggest naming the unit testing project Services.Test.

  1. Open Visual Studio and open your Harmony Core development solution.

  2. In Solution Explorer, right-click on the main solution and select Add > New Project....

  3. Locate and select the project template for a Synergy DBL Console App (.NET Core) and click the Next button.

  4. Set the Project name to Services.Test then click the Create button.

  5. The project will contain a default source file named Program.dbl, rename it to SelfHost.dbl.

Next, you need to check a couple of project configuration items, making sure that your project is targeting .NET Core 3.1, and also making the project subscribe to the Common Properties that are already in use in the other projects in your solution.

  1. In Solution Explorer, right-click on the new project and select Properties.

  2. In the Application tab, set the Target framework to .NET Core 3.1

  3. In the Common Properties tab, check the Use common properties option, then ensure that the Common properties file location is set to $(SolutionDir)Common.props, and that you can see various values in the properties list.

  4. Select File > Save All from the menu, then close the properties window.

Add Project References

Now that you have a basic project in place, you will need to add references to several of the other projects in your solution, so that your unit testing code can have access to your startup configuration, controllers and models, etc.

  1. In Solution Explorer, and within the Services.Test project, right-click on the References folder and select Add Reference...

  2. On the left side of the Reference Manager dialog, select the Projects node, then check the checkbox control next to the following projects:

    • Services
    • Services.Controllers
    • Services.Models
  3. Click the OK button to close the dialog and add the references.

Add Nuget Package References

In addition to adding references to several local projects, you will also on this occasion need to add references to the various NuGet packages that will be required in order to implement unit testing.

Adding references to NuGet packages is completely normal in .NET Core development, where pretty much everything comes from NuGet. But you probably haven't been exposed to this yet when performing Harmony Core development, because until now, pretty much everything you have done has been in the context of existing pre-configured projects that were provided by the Harmony Core Solution Templates.

  1. In Solution Explorer right click on the main solution and select Manage NuGet Packages for Solution….

  2. In the upper-right corner of the dialog, check that the Package source dropdown is set to nuget.org.

  3. In the NuGet - Solution window, if not already selected near the top-left corner, select the Installed tab.

  4. In the list of installed packages, locate and select the package Harmony.Core.AspNetCore.

  5. In the list of projects near the top-right of the dialog, check the checkbox next to the Services.Test.synproj project, then click the Install button

  6. Repeat the previous two steps for these additional packages. Note that for some packages a dialog may popup requiring that you accept the license terms of that package:

    • HarmonyCore.CodeDomProvider
    • IdentityModel (additional dialog to accept)
    • Microsoft.EntityFrameworkCore
    • Microsoft.NET.Test.Sdk (two additional dialogs to accept)
    • Nito.AsyncEx
    • System.Linq.DynamicCore
    • System.Text.Encoding.CodePages (additional dialog to accept)

Next you need to install some new packages that have not previously been used in the current solution, so they will be downloaded from NuGet.

  1. Switch from the Installed tab to the Browse tab, and in the Search control enter Microsoft.AspNetCore.Mvc.Testing

  2. Select the matching package in the packages list, it should be the top result, but double check.

  3. Check that Services.Test.synproj is still checked in the projects list, then click the Install button (again, there may be two dialogs to accept).

  4. Repeat the process for the following additional packages:

    • Microsoft.AspNetCore.OData
    • Microsoft.AspNetCore.SignalR.Client
    • Microsoft.Extensions.Logging.Console
    • MSTest.TestAdapter
    • MSTest.TestFramework
    • NewtonSoft.Json

Finally, you may need to update the version of two Synergy packages, the build package, and the runtime package. To check:

  1. Switch from the Browse tab to the Updates tab then in the Search control enter Synergex

You may see up to two packages in the list, Synergex.SynergyDE.Build and Synergex.Synergyde.synrnt.

  1. If you see either or both, check the Select all control just above the list.

  2. Look in the projects list to the top right, you should see that several of the projects in the list are checked.

  3. Expand the Version column so you can see the full version numbers.

  4. Look at the version numbers next to each project. If the Services.Test project is a different version than the other projects, then

    • Uncheck all of the other projects, leaving only the Services.Test project checked
    • In the Version drop-down below the list of projects, select the same version that the other projects are configured to. This may or may not be the highest version.
    • Click the Install button.

All of the projects should now be listed as running the same version.

  1. Close the Nuget - Solution window.

Run the Project Upgrade Tool

To ensure correct VERSIONS of the NuGet packages!!!

  1. harmonycore upgrade-latest
  2. YES

Build To Verify OK So Far

  1. Right click on the Services.Test project and select Build.

  2. Check the output window, you should see a successful build, something like this:

    1>------ Build started: Project: Services, Configuration: Debug Any CPU ------
    2>------ Build started: Project: Services.Test, Configuration: Debug Any CPU ------
    ========== Build: 2 succeeded, 0 failed, 4 up-to-date, 0 skipped ==========
    

Configure Code Generation

  1. Edit regen.bat and remove the rem comment from the ENABLE_UNIT_TEST_GENERATION, like this

    set ENABLE_UNIT_TEST_GENERATION=YES
    

Generate Code

  1. Save the file, open a command prompt, go to the solution folder and execute the batch file.
  2. Look for the DONE message to indicate that the code generation was successful.

What Changed

  • Self-hosting code was generated into the SelfHost.dbl source file
  • Several new source files were created in the Services.Test project folder:
    • TestConstants.Properties.dbl
    • TestConstants.Values.dbl
    • UnitTestEnvironment.dbl
  • A new folder named DataGenerators was created in the Services.Test project folder, and several source files were generated into the folder:
    • CustomerLoader.dbl
    • ItemLoader.dbl
    • OrderItemLoader.dbl
    • OrderLoader.dbl
    • VendorLoader.dbl
  • A new folder named Models was created in the Services.Test project folder, and several source files were generated into the folder:
    • Customer.dbl
    • Item.dbl
    • Order.dbl
    • OrderItem.dbl
    • Vendor.dbl
  • A new folder named UntTests was created in the Services.Test project folder, and several source files were generated into the folder:
    • CustomerTests.dbl
    • ItemTests.dbl
    • OrderItemTests.dbl
    • OrderTests.dbl
    • VendorTests.dbl

Add Code to Projects

Your next task is to add all of these new files to your Services.Test project:

  1. Right-click on the Services.Test project, select Add > Existing Item…, then select all of the .dbl files and click the Add button.

  2. Right-click on the Services.Test project, select Add > Existing Item…, then drill into the DataGenerators folder, select all of the .dbl files and click the Add button.

  3. Repeat the process for the Models folder.

  4. Repeat the process for the UnitTests folder.

Set Test Values

  1. Edit TestConstants.Testvalues.dbl

  2. Replace the assignment statements for Test data for Customer with the following code:

    GetCustomer_CustomerNumber = 1
    
    GetCustomer_Expand_REL_Orders_CustomerNumber = 1
    
    GetCustomer_Expand_REL_Item_CustomerNumber = 1
    
    GetCustomer_Expand_All_CustomerNumber = 1
    
    GetCustomer_ByAltKey_State_State = "CA"
    
    GetCustomer_ByAltKey_Zip_ZipCode = 94806
    
    GetCustomer_ByAltKey_PaymentTerms_PaymentTermsCode = "01"
    UpdateCustomer_CustomerNumber = 3
    
  3. Replace the assignment statements for Test data for Item with the following code:

    GetItem_ItemNumber = 1
    
    GetItem_Expand_REL_Vendor_ItemNumber = 1
    
    GetItem_Expand_REL_OrderItems_ItemNumber = 6
    
    GetItem_Expand_All_ItemNumber = 6
    
    GetItem_ByAltKey_VendorNumber_VendorNumber = 38
    
    GetItem_ByAltKey_Color_FlowerColor = "white"
    
    GetItem_ByAltKey_Size_Size = 10
    
    GetItem_ByAltKey_Name_CommonName = "Paper Mulberry"
    UpdateItem_ItemNumber = 22
    
  4. Replace the assignment statements for Test data for Order with the following code:

    GetOrder_OrderNumber = 3
    
    GetOrder_Expand_REL_OrderItems_OrderNumber = 3
    
    GetOrder_Expand_REL_Customer_OrderNumber = 3
    
    GetOrder_Expand_All_OrderNumber = 3
    
    GetOrder_ByAltKey_CustomerNumber_CustomerNumber = 1
    
    GetOrder_ByAltKey_DateOrdered_DateOrdered = new DateTime(2018,03,07)
    
    GetOrder_ByAltKey_DateCompleted_DateCompleted = new DateTime(2018,08,21)
    UpdateOrder_OrderNumber = 10
    
  5. Replace the assignment statements for Test data for OrderItem with the following code:

    GetOrderItem_OrderNumber = 3
    GetOrderItem_ItemNumber = 1
    
    GetOrderItem_Expand_REL_Order_OrderNumber = 3
    GetOrderItem_Expand_REL_Order_ItemNumber = 1
    
    GetOrderItem_Expand_REL_Item_OrderNumber = 3
    GetOrderItem_Expand_REL_Item_ItemNumber = 1
    
    GetOrderItem_Expand_All_OrderNumber = 3
    GetOrderItem_Expand_All_ItemNumber = 1
    
    GetOrderItem_ByAltKey_ItemOrdered_ItemOrdered = 6
    
    GetOrderItem_ByAltKey_DateShipped_DateShipped = new DateTime(2018,08,21)
    
    GetOrderItem_ByAltKey_InvoiceNumber_InvoiceNumber = 930301
    UpdateOrderItem_OrderNumber = 999999
    UpdateOrderItem_ItemNumber = 20
    
  6. Replace the assignment statements for Test data for Vendor with the following code:

    GetVendor_VendorNumber = 38
    
    GetVendor_Expand_REL_Items_VendorNumber = 40
    
    GetVendor_Expand_All_VendorNumber = 40
    
    GetVendor_ByAltKey_State_State = "MA"
    
    GetVendor_ByAltKey_Zip_ZipCode = 01000
    
    GetVendor_ByAltKey_PaymentTerms_PaymentTermsCode = ""
    UpdateVendor_VendorNumber = 39
    

Build the Code

Run Tests

  1. Open Command prompt
  2. Set SolutionDir
  3. Move to Services.Test folder
  4. dotnet test

Should see something like this:

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Test Run Successful.
Total tests: 59
     Passed: 59
 Total time: 30.8590 Seconds
Clone this wiki locally