Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I assert the count or the result explicitly?

[Test]
public void SimpleFlatten()
{
    // arrange
    var sentences = new List<string> { "Hello World", "Goodbye World" };

    // act
    var result = sentences.SelectMany(n => n.Split(' '));

    // assert
    Assert.That(result.Count(), Is.EqualTo(4));
}

Would testing the count of the result be plentiful here do you think?

Having thought about this logically, I feel as though checking the count should be enough because I have no reasonable suspicion that the SelectMany method will ever return the correct number unless the method is working correctly.

I am new to TDD however and wonder if being explicit like this would be a better practice?

CollectionAssert.AreEquivalent(
    new[] { "Hello", "World", "Goodbye", "World" }, result);
like image 263
Caster Troy Avatar asked Mar 19 '23 08:03

Caster Troy


1 Answers

Try to put yourself in the position of person reading this test 6 months from now. Which test carries more information?

Given collection of two sentences, "Hello World" and "Goodbye World"

  1. simple flatten returns collection of 4 words
  2. simple flatten returns "Hello", "World", "Goodbye", "World"

Which one tells you more about the purpose of the method or what the method does? From TDD standpoint, you should prefer to go for less ambiguous tests, which in this case is second one.

like image 57
k.m Avatar answered Apr 26 '23 07:04

k.m