C# Unit Tests

UmbNav includes comprehensive unit tests for the backend services using xUnit and Moq.

Test Project Structure

Umbraco.Community.UmbNav.Core.Tests/
├── Extensions/
│   └── UmbNavItemExtensionsTests.cs
├── Models/
│   └── UmbNavBuildOptionsTests.cs
├── Services/
│   └── UmbNavMenuBuilderServiceTests.cs
└── Umbraco.Community.UmbNav.Core.Tests.csproj

Running Tests

Command Line

# Run all tests
cd Umbraco.Community.UmbNav.Core.Tests
dotnet test

# Run with detailed output
dotnet test --verbosity normal

# Run with code coverage
dotnet test --collect:"XPlat Code Coverage"

# Run specific test class
dotnet test --filter "FullyQualifiedName~UmbNavMenuBuilderServiceTests"

# Run specific test
dotnet test --filter "DisplayName~WithEmptyItems"

Visual Studio

  1. Open Test Explorer (Test → Test Explorer)

  2. Click "Run All" or right-click specific tests

  3. View results in the Test Explorer window

VS Code

  1. Install the C# Dev Kit extension

  2. Open the Testing panel

  3. Click run/debug buttons next to tests

Test Examples

Testing the core service that processes menu items:

Testing Build Options

Testing Max Depth

Testing Authentication-Based Visibility

Testing Content Resolution

Testing Recursive Processing

Mocking Patterns

Mocking Published Content

Mocking HTTP Context

Testing Custom Extensions

When you create custom services, test them thoroughly:

Test Data Builders

Create reusable test data builders:

Best Practices

  1. One assertion per test (when practical)

  2. Descriptive test names - Method_Scenario_ExpectedResult

  3. Arrange-Act-Assert pattern - Clear structure

  4. Test edge cases - null, empty, boundary values

  5. Keep tests independent - No shared mutable state

  6. Use test data builders - Avoid repetitive setup code

Last updated

Was this helpful?