-
Notifications
You must be signed in to change notification settings - Fork 14
Tutorial 05 01 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.
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
.
-
Open Visual Studio and open your Harmony Core development solution.
-
In
Solution Explorer
, right-click on the main solution and selectAdd > New Project...
. -
Locate and select the project template for a
Synergy DBL Console App (.NET Core)
and click theNext
button. -
Set the
Project name
toServices.Test
then click theCreate
button. -
The project will contain a default source file named
Program.dbl
, rename it toSelfHost.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.
-
In
Solution Explorer
, right-click on the new project and selectProperties
. -
In the
Application
tab, set theTarget framework
to.NET Core 3.1
-
In the
Common Properties
tab, check theUse common properties
option, then ensure that theCommon properties file location
is set to$(SolutionDir)Common.props
, and that you can see various values in the properties list. -
Select
File > Save All
from the menu, then close the properties window.
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.
-
In
Solution Explorer
, and within theServices.Test
project, right-click on theReferences
folder and selectAdd Reference...
-
On the left side of the
Reference Manager
dialog, select theProjects
node, then check the checkbox control next to the following projects:- Services
- Services.Controllers
- Services.Models
-
Click the
OK
button to close the dialog and add the 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.
-
In
Solution Explorer
right click on the main solution and selectManage NuGet Packages for Solution…
. This will open a window with the tab titleNuget - Solution
-
In the upper-right corner of the dialog, check that the
Package source
dropdown is set tonuget.org
.
When considering the list of NuGet packages that will be required to implement the unit testing features that will be needed, those packages fall into two different categories. These categories are packages that are already used by other projects in the solution, and packages that are not. You will use a slightly different procedure to add those categories of packages.
As you proceed through the following steps, some packages that you add, regardless of whether they are already used in the solution or not, will display a popup dialog asking you to accept the license terms for the package. and depending on your Visual Studio configuration, sometimes you may see two dialogs displayed when to add a package. It's OK to just dismiss these dialogs. In fact, some have a don't show me this again
option, in case you want to suppress them from being displayed.
- In the
NuGet - Solution
window, if not already selected near the top-left corner, select theInstalled
tab.
The list of packages that you will install from this UI is:
- Harmony.Core.AspNetCore
- 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)
-
For each of the packages in the list:
- Locate and select the package in the list.
- In the list of projects near the top-right of the dialog, check the checkbox next to the
Services.Test.synproj
project. Once selected it should remain selected. - Click the
Install
button
As you add each package, you should see the package appear in Solution Explorer, in the Services.Test > References > Nuget
node.
The process for adding packages that are not already installed is similar, except that you must search for each package by name and then install it for the project.
- In the
Nuget - Solution
window, switch from theInstalled
tab to theBrowse
tab.
The list of packages that you will install from this UI is:
- Microsoft.AspNetCore.Mvc.Testing
- Microsoft.AspNetCore.OData
- Microsoft.AspNetCore.SignalR.Client
- Microsoft.Extensions.Logging.Console
- MSTest.TestAdapter
- MSTest.TestFramework
- NewtonSoft.Json
-
For each of the packages in the list:
- Type the package name into the
Search
control. - Wait for the search to complete, and look for the package. It should be the top item but always check the name.
- Select the package.
- Check that
Services.Test.synproj
is still checked in the projects list. - Click the
Install
button.
- Type the package name into the
-
When you are done with adding all of these package references, close the
Nuget - Solution
window.
The next step is to ensure that your new project is configured to use the correct versions of all of the NuGet packages, based on the latest version of Harmony Core. This can be done easily by running the Harmony Core Project Upgrade Tool.
-
Use Tools > Command Prompt (x64) to open a command prompt window.
-
In Visual Studio, use
File > Close Solution
to close the Visual Studio solution. -
Back in the command prompt window, move up one level to your main solution directory, and execute the following command:
cd .. harmonycore upgrade-latest
-
You will be required to enter
YES
to confirm the upgrade, and you should see output similar to this:Scanning 'D:\HC_WEBINAR_MyDemoApi' for HarmonyCore project files This utility will make significant changes to projects and other source files in your Harmony Core development environment. Before running this tool we recommend checking the current state of your development environment into your source code repository, taking a backup copy of the environment if you don't use source code control. Type YES to proceed: YES Updating template files in D:\HC_WEBINAR_MyDemoApi\Templates Found template files in D:\HC_WEBINAR_MyDemoApi\Templates\SignalR Found template files in D:\HC_WEBINAR_MyDemoApi\Templates\TraditionalBridge Updating traditional bridge files in D:\HC_WEBINAR_MyDemoApi\TraditionalBridge\Bridge
The primary purpose of running the project upgrade tool, in this case, was to ensure that appropriate versions of all dependent packages are being used, based on the version of Harmony Core being used.
However, if your solution was using an earlier version of Harmony Core then it will have been updated to the latest version, and appropriate CodeGen templates and Traditional Bridge library code (if in use) will have been provided also.
- Close the command prompt, then return to Visual Studio and re-open the solution.
-
Right click on the
Services.Test
project and selectBuild
. -
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 ==========
-
Edit
regen.bat
and remove therem
comment from theENABLE_UNIT_TEST_GENERATION
, like thisset ENABLE_UNIT_TEST_GENERATION=YES
- Save the file, open a command prompt, go to the solution folder and execute the batch file.
- Look for the
DONE
message to indicate that the code generation was successful.
- 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 theServices.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 theServices.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 theServices.Test
project folder, and several source files were generated into the folder:- CustomerTests.dbl
- ItemTests.dbl
- OrderItemTests.dbl
- OrderTests.dbl
- VendorTests.dbl
Your next task is to add all of these new files to your Services.Test
project:
-
Right-click on the
Services.Test
project, selectAdd > Existing Item…
, then select all of the.dbl
files and click theAdd
button. -
Right-click on the
Services.Test
project, selectAdd > Existing Item…
, then drill into theDataGenerators
folder, select all of the.dbl
files and click theAdd
button. -
Repeat the process for the
Models
folder. -
Repeat the process for the
UnitTests
folder.
-
Edit
TestConstants.Testvalues.dbl
-
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
-
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
-
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
-
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
-
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
- Open Command prompt
- Set SolutionDir
- Move to Services.Test folder
- 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
-
Tutorial 2: Building a Service from Scratch
- Creating a Basic Solution
- Enabling OData Support
- Configuring Self Hosting
- Entity Collection Endpoints
- API Documentation
- Single Entity Endpoints
- OData Query Support
- Alternate Key Endpoints
- Expanding Relations
- Postman Tests
- Supporting CRUD Operations
- Adding a Primary Key Factory
- Adding Create Endpoints
- Adding Upsert Endpoints
- Adding Patch Endpoints
- Adding Delete Endpoints
-
Harmony Core Code Generator
-
OData Aware Tools
-
Advanced Topics
- CLI Tool Customization
- Adapters
- API Versioning
- Authentication
- Authorization
- Collection Counts
- Customization File
- Custom Field Types
- Custom File Specs
- Custom Properties
- Customizing Generated Code
- Deploying to Linux
- Dynamic Call Protocol
- Environment Variables
- Field Security
- File I/O
- Improving AppSettings Processing
- Logging
- Optimistic Concurrency
- Multi-Tenancy
- Publishing in IIS
- Repeatable Unit Tests
- Stored Procedure Routing
- Suppressing OData Metadata
- Traditional Bridge
- Unit Testing
- EF Core Optimization
- Updating a Harmony Core Solution
- Updating to 3.1.90
- Creating a new Release
-
Background Information