Search code examples
c#xunitfluent-assertions

FluentAssertions - Check if all properties have default values when given empty input


As you can see in the unit test below, I'm basically checking if all properties have default values. Is there a more fluent way in FluentAssertions to do so that I'm not aware of?

This test aims to check whether it throws or not when given no additional information.

public class OhlcvBuilderTests
{
    // Happy path
    [Fact]
    public void Build_ShouldBeConstructed_WhenGivenEmptyInput()
    {
        // Arrange
        var ohlcvBuilder = new OhlcvBuilder();
        var expectedOhlcv = new
        {
            Date = DateTimeOffset.MinValue,
            Open = 0,
            High = 0,
            Low = 0,
            Close = 0,
            Volume = 0
        };

        // Act
        var ohlcv = ohlcvBuilder.Build();

        // Assert
        ohlcv.Should().BeEquivalentTo(expectedOhlcv);
    }
}

Solution

  • I guess that's what I could think of. Thanks to @Matthew Watson's comment.

    public class OhlcvBuilderTests
    {
        [Fact]
        public void Build_ShouldNotThrow_WhenGivenNoAdditionalSetupInformation()
        {
            // Arrange
            var ohlcvBuilder = new OhlcvBuilder();
    
            // Act
            var action = new Action(() => ohlcvBuilder.Build());
    
            // Assert
            action.Should().NotThrow();
        }
    
        [Theory]
        [InlineData(0, 0, 0, 0, 0)]
        [InlineData(15000, 16400, 13500, 16000, 76000)]
        public void Build_ShouldBeConstructed_WhenGivenSetupInformation(decimal open, decimal high, decimal low, decimal close, decimal volume)
        {
            // Arrange
            var ohlcvBuilder = new OhlcvBuilder();
            var expectedOhlcv = new
            {
                Open = open,
                High = high,
                Low = low,
                Close = close,
                Volume = volume
            };
    
            // Act
            var ohlcv = ohlcvBuilder
                .WithOpen(open)
                .WithHigh(high)
                .WithLow(low)
                .WithClose(close)
                .WithVolume(volume)
                .Build();
    
            // Assert
            ohlcv.Should().BeEquivalentTo(expectedOhlcv);
        }
    
        [Fact]
        public void Build_ShouldThrow_WhenGivenNegativeOpen()
        {
            // Arrange
            const decimal open = -1;
            var ohlcvBuilder = new OhlcvBuilder();
    
            // Act
            var action = new Action(() => ohlcvBuilder.WithOpen(open));
    
            // Assert
            action.Should().Throw<ArgumentOutOfRangeException>()
                .WithMessage("The * cannot be null. (Parameter '*')");
        }
    }