Skip to content

Tests : Domain Layer

  • Do create a unit tests for your Manager suffixed services.

    • Do create Managers folder in GridLab.PSSX.<ModuleName>.Domain.Tests project.

    • Do create an unit test for each domain service with <DomainModelName>Manager_Tests name.

    In this case it is IdentitySessionManager_Tests.

    public class IdentitySessionManager_Tests : IdentityDomainTestBase
    {
        private readonly IIdentitySessionRepository _identitySessionRepository;
        private readonly IIdentitySessionManager _identitySessionManager;
        private readonly IdentitySessionTestData _identitySessionTestData;
    
        public IdentitySessionManager_Tests()
        {
            _identitySessionRepository = GetRequiredService<IIdentitySessionRepository>();
            _identitySessionManager = GetRequiredService<IIdentitySessionManager>();
            _identitySessionTestData = GetRequiredService<IdentitySessionTestData>();
        }
    
        [Fact]
        public async Task CreateAsync_Should_Create_Session()
        {
            // Arrange
            var sessionId = "session1";
            var device = "device1";
            var userId = Guid.NewGuid();
            var tenantId = Guid.NewGuid();
            var clientId = "client1";
            var ipAddresses = "127.0.0.1";
    
            // Act
            var result = await _identitySessionManager.CreateAsync(sessionId, device, null, userId, tenantId, clientId, ipAddresses);
    
            // Assert
            result.ShouldNotBeNull();
            result.SessionId.ShouldBe(sessionId);
        }
    
        ... add more unit test depending on your domain manager behaviour.
    } 
    

    The tests should cover the key functionalities of the IIdentitySessionManager interface.

  • Do not use constructor injection for tests.

    Using GetRequiredService provides more flexibility in setting up the test environment. It allows the test class to resolve services at runtime, which can be useful if the services need to be configured or modified before being used in tests.

  • Do not create unit test for private and protected methods of class. You should test behaviour not the methods.