Skip to content

Short demo of designing testable code in .NET Core with interfaces, dependency injection and IoC

Notifications You must be signed in to change notification settings

rsobon/TestableCode

Repository files navigation

TestableCode

Short demo of designing testable code in .NET Core with interfaces, dependency injection and IoC

Using:

  • .NET 6
  • Microsoft Dependency Injection
  • Moq
  • Fluent Assertions
  • NUnit

What is testable code?

  • Easy to test
  • Built from isolated components
  • Clean architecture

Advantages of testable code

  • Easy to create unit tests
  • Usually easier to create other tests as well (UI, integration etc.)
  • Readable code
  • Self-documenting (unit tests are good documentation)
  • Easy to refactor (interchangeable components)
  • Reduce fear of change
  • Maintainable code

Disadvantages of testable code

  • Writing code takes more time
  • Increased complexity (more abstraction layers)

Symptoms of not testable code

  • “New” keyword (using new on ValueObjects, Dictionaries, Lists is OK).
  • Static classes & methods (DateTime, File, Path)
  • Framework methods
  • Long methods and classes
  • Global variables
  • Complicated constructors
  • Chains of methods (accessing dependencies of dependencies)

About

Short demo of designing testable code in .NET Core with interfaces, dependency injection and IoC

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages