KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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? To maximize the testability of code, follow these rules: Write the test first, then the code. Reason: This ensures that you write testable code and that every line of code gets tests written for it. Design classes using dependency injection. Reason: You cannot mock or test what cannot be seen. Separate UI code from its behavior using Model-View-Controller or Model-View-Presenter. Reason: Allows the business logic to be tested while the parts that can't be tested (the UI) is minimized. Do not write static methods or classes. Reason: Static methods are difficult or impossible to isolate and Rhino Mocks is unable to mock them. Program off interfaces, not classes. 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. Isolate external dependencies. Reason: Unresolved external dependencies cannot be tested. Mark as virtual the methods you intend to mock. Reason: Rhino Mocks is unable to mock non-virtual methods.
Tags (comma-separated)
Save Edits
Cancel