jeudi 1 novembre 2018

Almost dpublicated unit test

Suppose I have the following class:

public class Test
{
    public Test(int value)
    {
        Value = value;
    }

    public int Value { get; }
    public string ValueDescription { get { return Value > 10 ? "Value bigger than 10" : default(string); } }
}

What would be the best approach to test the ValueDescription logic? I only see two ways of testing it: one using expected result as a parameter

[TestFixture]
public class TestTest
{
    [Test]
    [TestCase(12, false)]
    [TestCase(10, false)]
    [TestCase(9, true)]
    [TestCase(1, true)]
    public void Should_ReturnValueDescription_AccordingToValue(int? value, bool isNullResult)
    {
        var mockTest = new Test(value);

        Assert.AreEqual(string.IsNullOrEmpty(mockTest.ValueDescription), isNullResult);
    }
}

And the other one, "duplicating" part of the test code(can't see it quite explicit here, because its quite a simple example), just changing the Assert method.

 [Test]
    [TestCase(12)]
    [TestCase(50)]
    [TestCase(100)]
    [TestCase(300)]
    public void Should_ReturnValueDescription_When_ValueBiggerThan10(int? value, bool isNullResult)
    {
        var mockTest = new Test(value);

        Assert.IsNotNull(mockTest.ValueDescription);
    }

 [Test]
    [TestCase(9)]
    [TestCase(2)]
    [TestCase(5)]
    [TestCase(10)]
    public void Should_ReturnValueDescription_When_ValueLowerOrEqualThan10(int? value, bool isNullResult)
    {
        var mockTest = new Test(value);

        Assert.IsNull(mockTest.ValueDescription);
    }

Considering this was a quite simplistic example, what would be the best approach in this, and in a more complex scenario? (with the need of stubs and etc to test it)

Aucun commentaire:

Enregistrer un commentaire