1

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);
    }
}
Hulkstance
  • 1,323
  • 9
  • 15
  • Can I ask what you’re trying to test here? It would seem that you’re testing the functionality of how C# works rather than your own logic? Btw, Fluent Assertions doesn’t have this behaviour out of the box, I would just make an extension for it. – scottdavidwalker Oct 22 '22 at 13:01
  • @scottdavidwalker, in the unit tests, you usually have at least one test which is the happy path and the other tests are invalid input parameters, empty input, etc. That's what I'm trying to achieve – Hulkstance Oct 22 '22 at 13:09
  • 1
    I disagree that there should be a happy path in all tests. All you’re doing here is asserting that C# functionality works. It would be different if a specific property had to be default for some reason because then you’ve got a reason to assert that it must be default. Otherwise this just feels like a bit of a wasted test. Personal opinion of course – scottdavidwalker Oct 22 '22 at 13:13
  • 2
    @scottdavidwalker In this case it's testing that `OhlcvBuilder.Build()` works correctly when not passed any additional setup information, so I think that's a useful test. For example, it would catch the case where `OhlcvBuilder.Build()` had a bug where it threw an exception for some reason when no additional setup data is specified. – Matthew Watson Oct 22 '22 at 13:20
  • @MatthewWatson, yup, very well said. – Hulkstance Oct 22 '22 at 13:24
  • @MatthewWatson but then you could just well use XUnit’s record class and assert that no exception was thrown? Just the whole checking default values doesn’t sit right in my head. – scottdavidwalker Oct 22 '22 at 17:15

2 Answers2

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);
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
0

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

Hulkstance
  • 1,323
  • 9
  • 15