Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dependency Injection & Unit Testing

Ed0195c951a51ca754ba19eb92a98e3f?s=47 Chris Ayers
February 19, 2019

Dependency Injection & Unit Testing

A dive into dependency injection in traditional .Net applications and ASP.Net Core. Followed by how dependency injection enables unit testing in .Net core and EF core. Code examples of how to unit test at the controller, service, and data layers.


Chris Ayers

February 19, 2019


  1. The perfect pair Dependency Injection & Unit Testing

  2. Chris Ayers Linkedin: /in/chris-l-ayers/ Twitter: @Chris_L_Ayers Projects: https://github.com/Codebytes /UnitTestingDemo https://github.com/Codebytes

  3. Topics • Dependency Injection • Why? • SOLID • Design

    Patterns • Types • Features • DEMO • Unit Testing • Terms • Test Layout and Naming • Mocking • Best Practices • DEMO
  4. Why? • Clarity • Maintainability • Reuseability • Modularity •

    Testability • Use Different Implementations
  5. SOLID • Single Responsibility Principle • Open/Closed Principle • Liskov

    Substituion Principle • Interface Segregation Principle • Dependency Inversion Principle
  6. Patterns • Façade • Factory • Strategy • Visitor

  7. No Dependency Injection • Not using interfaces to reduce coupling

    • Hard to test • No clear understanding of relationships between different classes
  8. Constructor Injection • Clear understanding of dependencies

  9. Property Injection • Initialization not guaranteed • Can be good

    for bigger/complex objects
  10. Method Parameter Injection • When a dependency is used for

    only method and is an edge case
  11. Dependency Injection - Scopes Transient Scoped Singleton

  12. Dependency Injection - Features Configuration Configuration Scopes Scopes Factory Support

    Factory Support
  13. Dependency Injection - Frameworks .Net Core .Net Core Unity Unity

    Castle Windsor Castle Windsor Ninject Ninject AutoFac AutoFac SimpleInjector SimpleInjector Spring.NET Spring.NET StructureMap StructureMap
  14. Dependency Injection - DEMO

  15. Decoupling is essential for unit testing. DI is a great

    way to achieve decoupling. Dependency Injection + Unit Testing
  16. Unit Testing – Terms (SUT) System Under Test or (CUT)

    Class Under Test Test Doubles Fake Mock Stub Spy Dummy Objects
  17. Unit Testing - Architectures • Easy to Test Code •

    Inversion of Control (IoC) • Interface Segregation • Functional Programming • Hard to Test code • It is tightly coupled to the concrete implementation. • It violates the Single Responsibility Principle. • It lies about its dependencies. • It is hard to understand and maintain.
  18. Unit Testing - Layout Split up tests by assembly/project. •

    Ex: if you have two libraries: foo.dll, bar.dll • have 2 unit test assemblies: foo.tests.dll, bar.tests.dll Try to have at least 1 test class per class • Ex: if you have two classes in project foo: a.cs, b.cs • have 2 unit test classes in project foo.tests: aTests.cs, bTests.cs • If there are a lot of methods, or tests for some methods, split it up if there are a too many tests in one class. Use folders to mirror layout and namespaces of target project
  19. Unit Testing - Naming Name test class for the class

    under test • Foo.cs -> FooTests.cs [UnitOfWork_StateUnderTest_ExpectedBehavior] Examples • public void GetLines_twoLineString_returnsTwoStrings() • public void GetNonWhitespaceLines_null_Throws() • public void ReplaceMultiple_stringWithHashNonMatchingMappings_String() • public void ReplaceMultiple_stringMatchingMappings_UpdatedString()
  20. Unit Testing – Frameworks Nunit Xunit MStest Moq NSubstitute JustMockIt

  21. Unit Testing – Gotchas Usually only works for Interfaces or

    Virtual Methods. Statics can be difficult, refactoring can help or Microsoft Fakes Mocking can make assumptions about boundaries
  22. Unit Testing – Best Practices AAA Arrange Act Assert One

    Assert Per Test Method Avoid Test Interdependence Keep It Short, Sweet, and Visible Recognize Test Setup Pain as a Smell Add Them to the Build
  23. Unit Testing – What is a good test? Fast Fast

    Isolated Isolated Repeatable Repeatable Self- Checking Self- Checking Timely Timely
  24. Unit Testing - DEMO

  25. Questions? I might have answers