Reputation: 2866
[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);
Upvotes: 4
Views: 1134
Reputation: 31464
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"
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.
Upvotes: 6
Reputation: 3915
Usually it would be a good practice to separate the method you want to test from the testCase, in your posted snippet, the whole implementation of the logic is in the test Case, what happens if in the real code the logic will change? the test case will have to be manually changed, causing issues.
Hence I'd advise you to first separate the logic of the method from the test case itself. Then, ask yourself, "What is it that I want to test?", if the purpose of the test is to verify the number of elemenets returned, your first assert is okay, in case what you want to test, and what's important, is the content of the returned list, then you have to test the content of the list, because you can't make assumptions on the implementation (also considering that it could change).
Upvotes: 1