Hulkstance
Hulkstance

Reputation: 1445

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);
    }
}

Upvotes: 1

Views: 1023

Answers (2)

Hulkstance
Hulkstance

Reputation: 1445

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 '*')");
    }
}

Upvotes: 0

Vivek Nuna
Vivek Nuna

Reputation: 1

Whatever you have mentioned is the correct way for your requirement. You can even do without using fluent assertions well with the below trick. simply get the JSON strings for both objects and compare them.

var json1 = JsonConvert.SerializeObject(object1);
var json2 = JsonConvert.SerializeObject(object2);

Assert.AreEqual(object1Json, object2Json);

Upvotes: 2

Related Questions