-
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…
. -
In the upper-right corner of the dialog, check that the
Package source
dropdown is set tonuget.org
. -
In the
NuGet - Solution
window, if not already selected near the top-left corner, select theInstalled
tab. -
In the list of installed packages, locate and select the package
Harmony.Core.AspNetCore
. -
In the list of projects near the top-right of the dialog, check the checkbox next to the
Services.Test.synproj
project, then click theInstall
button -
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.
-
Switch from the
Installed
tab to theBrowse
tab, and in theSearch
control enterMicrosoft.AspNetCore.Mvc.Testing
-
Select the matching package in the packages list, it should be the top result, but double check.
-
Check that
Services.Test.synproj
is still checked in the projects list, then click theInstall
button (again, there may be two dialogs to accept). -
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:
- Switch from the
Browse
tab to theUpdates
tab then in the Search control enterSynergex
You may see up to two packages in the list, Synergex.SynergyDE.Build
and Synergex.Synergyde.synrnt
.
-
If you see either or both, check the
Select all
control just above the list. -
Look in the projects list to the top right, you should see that several of the projects in the list are checked.
-
Expand the Version column so you can see the full version numbers.
-
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.
- Uncheck all of the other projects, leaving only the
All of the projects should now be listed as running the same version.
- Close the
Nuget - Solution
window.
To ensure correct VERSIONS of the NuGet packages!!!
- harmonycore upgrade-latest
- YES
-
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