title: Best Practices of Test Driven Development Using C# and RhinoMocks
body: <p>In order to help my team write testable code, I came up with this simple list of best practices for making our C# code base more testable. (Some of the points refer to limitations of Rhino Mocks, a mocking framework for C#, but the rules may apply more generally as well.)  Does anyone have any best practices that they follow?</p>
  
  <p>To maximize the testability of code, follow these rules:</p>
  
  <ol>
  <li><p><strong>Write the test first, then the code.</strong> Reason: This ensures that you write testable code and that every line of code gets tests written for it.</p></li>
  <li><p><strong>Design classes using dependency injection.</strong> Reason: You cannot mock or test what cannot be seen.</p></li>
  <li><p><strong>Separate UI code from its behavior using Model-View-Controller or Model-View-Presenter.</strong> Reason: Allows the business logic to be tested while the parts that can't be tested (the UI) is minimized.</p></li>
  <li><p><strong>Do not write static methods or classes.</strong>  Reason: Static methods are difficult or impossible to isolate and Rhino Mocks is unable to mock them.</p></li>
  <li><p><strong>Program off interfaces, not classes.</strong> Reason: Using interfaces clarifies the relationships between objects. An interface should define a service that an object needs from its environment.  Also, interfaces can be easily mocked using Rhino Mocks and other mocking frameworks.</p></li>
  <li><p><strong>Isolate external dependencies.</strong> Reason: Unresolved external dependencies cannot be tested.</p></li>
  <li><p><strong>Mark as virtual the methods you intend to mock.</strong> Reason: Rhino Mocks is unable to mock non-virtual methods.</p></li>
  </ol>