standards-c-sharp

Keep application functions pure if possible

Don’t

Here the Multiply() function depends on another property. We require an instance of the class, and to set up its property before we can use it. And this is reflected in the test.

public class SillyMultiplier
{
    public decimal Multiplier { get; set; }
    
    public decimal Multiply(decimal number)
    {
        return number * Multiplier;
    }
}

[Test]
public void Multiply_WithMultiplierSet_ReturnsArgumentMultipliedByMultiplier()
{
    var m = new SillyMultiplier { Multiplier = 5 };
    var result = m.Multiply(10);
    Assert.AreEqual(50, result);
}

Do

Here, because the function is pure, we don’t need to even create an instance of the class or set anything up to test it.

public static class SlightlyLessSillyMultiplier
{
    public static decimal Multiply(decimal number1, decimal number2)
    {
        return number1 * number2;
    }
}

[Test]
public void Multiply_MultipliesTwoNumbersCorrectly()
{
    var result = SlightlyLessSillyMultiplier.Multiply(5, 10);
    Assert.AreEqual(50, result);
}