From 4cdc81ebb89a7013fa1a62631f50b7b025961710 Mon Sep 17 00:00:00 2001 From: Marcin Kanar Date: Thu, 28 Mar 2024 20:45:54 +0100 Subject: [PATCH] Added unit tests for CategoryName value object --- .../ValueObjects/CategoryNameTests.cs | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 server/tests/Questeloper.Tests.Unit/DomainTests/ValueObjects/CategoryNameTests.cs diff --git a/server/tests/Questeloper.Tests.Unit/DomainTests/ValueObjects/CategoryNameTests.cs b/server/tests/Questeloper.Tests.Unit/DomainTests/ValueObjects/CategoryNameTests.cs new file mode 100644 index 0000000..8ecc444 --- /dev/null +++ b/server/tests/Questeloper.Tests.Unit/DomainTests/ValueObjects/CategoryNameTests.cs @@ -0,0 +1,87 @@ +using FluentAssertions; +using Questeloper.Domain.Exceptions; +using Questeloper.Domain.ValueObjects; + +namespace Questeloper.Tests.Unit.DomainTests.ValueObjects; + +public class CategoryNameTests +{ + [Fact] + public void Constructor_WhenValueIsNull_ShouldThrowValueIsEmptyException() + { + // Act + var act = () => new CategoryName(null); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void Constructor_WhenValueIsWhitespace_ShouldThrowValueIsEmptyException() + { + // Act + var act = () => new CategoryName(" "); + + // Assert + act.Should().Throw(); + } + + [Theory] + [InlineData("abc")] + public void Constructor_ForValidValue_ShouldNotThrow(string validValue) + { + // Act + var act = () => new CategoryName(validValue); + + // Assert + act.Should().NotThrow(); + } + + [Theory] + [InlineData("ab")] + public void Constructor_WhenValueIsIncorrectLength_ShouldThrowValueIncorrectLengthException(string invalidValue) + { + // Act + Action act = () => new CategoryName(invalidValue); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void ImplicitConversion_ToString_ShouldReturnCorrectStringValue() + { + // Arrange + var categoryName = new CategoryName("Programming"); + + // Act + string result = categoryName; + + // Assert + result.Should().Be("Programming"); + } + + [Fact] + public void ExplicitConversion_FromString_ShouldCreateValidObject() + { + // Act + var categoryName = (CategoryName)"Design"; + + // Assert + categoryName.Should().NotBeNull(); + categoryName.Value.Should().Be("Design"); + } + + [Fact] + public void ToString_Invoked_ShouldReturnCorrectValue() + { + // Arrange + var categoryName = new CategoryName("Art"); + + // Act + var result = categoryName.ToString(); + + // Assert + result.Should().Be("Art"); + } +}