NeuCharFramework (NCF)
  • NCF - NeuCharFramework
  • Projects

    • Preparation
    • Basic Library Source Code Analysis
    • DynamicWebApiEngine
    • Deployment
  • Help

    • Online Resources
    • Q&A Community
    • QQ Group (147054579)
    • Senparc WeChat SDK
  • Gitee
  • GitHub
  • English
  • 简体中文
GitHub
  • NCF - NeuCharFramework
  • Projects

    • Preparation
    • Basic Library Source Code Analysis
    • DynamicWebApiEngine
    • Deployment
  • Help

    • Online Resources
    • Q&A Community
    • QQ Group (147054579)
    • Senparc WeChat SDK
  • Gitee
  • GitHub
  • English
  • 简体中文
GitHub
  • NCF Overview

    • NCF - NeuCharFramework
    • About NCF
    • Environment Requirements
    • Frontend and Backend Separation Mode
    • Introduction to Xncf Modules
  • Prepare Development

    • Get NCF Template Source Code
    • Using Visual Studio to Run NCF
    • Running NCF Using CLI
    • Installation
    • Admin Login
    • Admin Backend
    • appsettings.json Configuration
    • Module Management
    • Accessing Documentation
  • Configuration

    • Entry File
    • Database Settings
    • appsettings.json Configuration
    • Docker
    • Dapr
    • Configure Multi-Tenant
    • Redis Cache
  • Modular Development

    • NCF Modular Development Concept
    • Composition of Xncf
    • Create the First Xncf Module
    • Xncf Module Sample Detailed Explanation
    • Implement Your Own Business Logic
    • Update Xncf Module
    • How to Call Between Modules
    • Publish Xncf Module to nuget.org
    • Update Base Library
    • Xncf Module Development
    • Embedding Static Resource Files into NCF
    • Publish Local Nuget Package
    • Advanced
  • Database

    • Database Settings
    • Multi-Database Support
    • Specify Database
    • Update database migration files for the Senparc.Service project (Migrations)
    • Multi-Database Principle
    • DatabasePlant
    • Tarmac Operation Database Migration and Update
  • Unit Testing

    • NCF Unit Test Introduction
    • Start Development
    • Advanced
    • Appendix
  • Q&A

    • NCF Terminology
    • NCF FAQ
  • Release

    • New Features
    • Upgrade Guide
    • Logs

Start Development

Preparation

NCF unit testing is based on the official .NET unit testing framework (MsTest), so you do not need to acquire any additional skills. Therefore, you only need to install the .NET regular development environment (.NET SDK) to develop and run unit tests.

Create Unit Test Project

You can create a unit test project using the regular command line method or the unit test project template in Visual Studio.

Taking the Admin module of the NCF project as an example, you can create a unit test project using the following command line:

dotnet new mstest -n Senparc.Areas.Admin.Tests

Add References to the Project

The unit test project needs to reference the project being tested, such as the Senparc.Areas.Admin project in the current example:

<ItemGroup>
<ProjectReference Include="..\..\Senparc.Areas.Admin\Senparc.Areas.Admin.csproj" />
</ItemGroup>

Secondly, we need to reference the NCF unit test base support library:

dotnet add package Senparc.Ncf.UnitTestExtension

After completion, you can see that the relevant code has been added to Senparc.Areas.Admin.Tests.csproj (you can also add it manually), such as:

<PackageReference Include="Senparc.Ncf.UnitTestExtension" Version="0.1.1.2-preview1" />

Create Unit Test File

You can create a unit test file according to the regular unit test rules, such as:

[TestClass]
public class AdminUserInfoServiceTests : BaseNcfUnitTest
{
}

BaseNcfUnitTest is the unit test base class provided by NCF, from the Senparc.Ncf.UnitTestExtension package installed in the previous step.

To provide common objects for the entire Service, we can add a constructor and private variables:

AdminUserInfoService adminUserInfoService;
public AdminUserInfoServiceTests()
{
    // Create a Mock object for the Repository base class
    var mockBaseRepo = base.GetRespository<AdminUserInfo>().MockRepository;
    // Since AdminUserInfoService creates a dedicated interface, the relevant extended interface is automatically mapped from the base class. If you directly use IClientRepositoryBase<AdminUserInfo> to operate data, this line can be omitted
    var mockAdminUserInfoRepo = base.CreateMockForExtendedInterface<IAdminUserInfoRepository, IClientRepositoryBase<AdminUserInfo>>(mockBaseRepo);
    // Generate AdminUserInfoService class
    adminUserInfoService = new AdminUserInfoService(mockAdminUserInfoRepo.Object, null, base._serviceProvider);
}

The above method is general and can be used for almost all Services and entity classes.

Next, we can test all the methods in AdminUserInfoService, for example:

[TestMethod]
public async Task CreateAdminUserInfoTest()
{
    var adminUserInfoDto = new CreateOrUpdate_AdminUserInfoDto()
    {
        UserName = "NCF_Admin",
        Password = "abcd",
    };

    var obj = await adminUserInfoService.CreateAdminUserInfoAsync(adminUserInfoDto);

    Assert.IsNotNull(obj);
    Assert.AreEqual(adminUserInfoDto.UserName, obj.UserName);

    var storedPassword = obj.GetSHA512Password(adminUserInfoDto.Password, obj.PasswordSalt);
    Assert.AreEqual(storedPassword, obj.Password);
}

The above code is relatively easy to understand:

  • adminUserInfoDto creates a DTO object for creating an admin
  • The adminUserInfoService.CreateAdminUserInfoAsync() method is the target of this unit test, used to implement admin addition
  • The Assert method verifies the expected results

Running the unit test shows that the test passed:

Edit this page on GitHub
Last Updated:
Contributors: Jeffrey Su
Prev
NCF Unit Test Introduction
Next
Advanced