What is a Unit testable C# code architecture

Priscilla Stephan
2 min readJun 26, 2021

There is one universal tip for writing a unit-testable code. You should just use the SOLID principle.

SOLID design principles

One of the most well-known collections of principles in the software engineering industry is the SOLID principles, documented by Robert C. Martin (also known as Uncle Bob).

This collection of principles can help your code to be more modular and to have increased testability. Let’s go deeper into each of these principles:

  1. Single Responsibility Principle (SRP)
  2. Open/Closed Principle (OCP)
  3. Liskov Substitution Principle (LSP)
  4. Interface Segregation Principle (ISP)
  5. Dependency Inversion Principle (DIP)

Dependency Inversion Principle (DIP)

Following this principle allows you to easily replace certain implementations with compatible ones which follow the same interface.

This is very useful for testing as it allows you to replace real implementations with test doubles. It also allows you to better react to changing requirements.

The following diagram shows how the example presented in the Single Responsibility Principle section also follows the Dependency Inversion Principle. You can see that both high-level and low-level modules depend on abstractions.

Refactoring Architecture Strategy:

  • Create interfaces for ALL classes in the code
  • Move from Concrete classes to using the DI container
  • Unit testable architecture
  • Start unit testing the best-designed classes
  • Refactor badly designed classes
  • Enhance our code coverage

--

--