Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To make TDD work as intended, you start from the simplest aspect of the unit under test.</p> <p>Your first test ought to be:</p> <pre><code>[TestMethod] public void GetEmployeesReturnsAList() { List&lt;Employee&gt; result = MyClass.GetEmployees(); Assert.IsNotNull(result); } </code></pre> <p>And implement like so:</p> <pre><code>public static List&lt;Employee&gt; GetEmployees() { return new List&lt;Employee&gt;(); } </code></pre> <p>Now this test should fail before you add the code to return the list, and from then on never fail again through all of the refactoring and adding of code to the unit under test.</p> <p>The next test would be something like:</p> <blockquote> <p>// This test is WRONG!! It is missing the setup of the available employees</p> <p>My intent was to highlight that returning 1 employee is a simpler case than returning the particular employee that is expected. Make sure to start with the simpler case.</p> <p>// [TestMethod] // public void GetEmployeesReturnsOneEmployeeWhenThereIsOneEmployeeAvailable() //<br> { // List result = MyClass.GetEmployees(); //<br> Assert.AreEqual(1, result.Count); // }</p> <p>//</p> </blockquote> <p>Run and the test fails. Then refactor like so:</p> <pre><code>public static List&lt;Employee&gt; GetEmployees() { Employee emp = new Employee(); List&lt;Employee&gt; empList = new List&lt;Employee&gt;(); empList.Add(emp); return empList; } </code></pre> <p>And now the test passes.</p> <p>Next test might be:</p> <pre><code>[TestMethod] public void GetEmployeesReturns_The_OneEmployeeWhenThereIsOneEmployeeAvailable() { // Arrange Employee emp = new Employee(); // Add code here to insert emp into the source of the list // This may be a mock. // Action List&lt;Employee&gt; result = MyClass.GetEmployees(); Assert.AreEqual(1, result.Count); Assert.AreSame(emp, result[0]); } </code></pre> <p>And now you add and re-write the code to make it pass without breaking any of the previous unit tests.</p> <p>Rinse and repeat, adding the least amount of code possible to make the tests pass without breaking any other tests. You will have to re-write the code many times, but the result is that you get the simplest code possible that satisfies all of your tests. And if the requirements are completely represented by all of your tests, you now have a fully functional unit.</p> <p>The key with TDD is that it prevents unnecessary complexity which usually comes about from making your code more abstract than it needs to be or before you discover a need for the complexity. You can always go in after-the-fact to make the code more abstract when a need is there. Note that a legitimate need may even be to satisfy code analysis tools.</p> <p>But you are free to re-factor without fear because you have great test coverage.</p> <p>One more thing with TDD is that you may want to augment these tests with technical edge case tests (passing null arguments and testing for the proper exception). However, this is not necessary and this is another benefit of TDD. </p> <p>It highlights things that many developers think of as a "best practices", such as checking method arguments for null inside the method when those checks are not necessary because the calling code should never pass in a null. And if the calling code does pass a null, the problem lies with it and not with the code inside the method.</p> <p>If there is a real code path that allows a null, and your method does not expect a null, then the calling code must perform the null check itself and do something other than call your method.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload